-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from kizza/ha-target-selector
Introduce target selector for lights
- Loading branch information
Showing
12 changed files
with
311 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { HomeAssistant } from "custom-card-helpers" | ||
|
||
type Area = { | ||
name: string | ||
} | ||
|
||
type Device = { | ||
name: string; | ||
name_by_user: string | ||
} | ||
|
||
type HomeAssistantWithExtras = HomeAssistant & { | ||
areas: Record<string, Area> | ||
devices: Record<string, Device> | ||
} | ||
|
||
export const toFriendlyName = ( | ||
identifier: 'area_id' | 'device_id' | 'entity_id', | ||
_hass: HomeAssistant, | ||
) => (value: string): string => { | ||
const hass = _hass as HomeAssistantWithExtras | ||
let name: string | undefined = undefined | ||
|
||
if (identifier == 'area_id') { | ||
name = hass.areas[value].name | ||
} else if (identifier == 'device_id') { | ||
name = hass.devices[value].name_by_user || hass.devices[value].name | ||
} else if (identifier == 'entity_id') { | ||
name = toState(hass)(value).attributes.friendly_name | ||
} | ||
|
||
return name || `Unknown (${value})` | ||
} | ||
|
||
export const toState = (hass: HomeAssistant) => (value: string) => hass.states[value] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { BaseConfig, Colour, HassServiceTarget, MagicHomePartyConfig } from "../types" | ||
|
||
export const parseConfig = (input: BaseConfig): MagicHomePartyConfig => | ||
withTargets(withSpeed(withColours(input))) | ||
|
||
const withColours = <T>(config: T): T & {colours: Colour[]} => ({ | ||
colours: [], | ||
...config, | ||
}) | ||
|
||
const withSpeed = <T>(config: T): T & {speed: number} => ({ | ||
speed: 20, | ||
...config, | ||
}) | ||
|
||
const withTargets = <T>(config: T): T & {targets: HassServiceTarget} => { | ||
// Has targets already | ||
if ((config as any).targets !== undefined) { | ||
return config as T & {entities: [], targets: HassServiceTarget} | ||
// Migrate entities to targets | ||
} else if ((config as any).entities !== undefined) { | ||
return { | ||
...config, | ||
targets: { | ||
entity_id: (config as any).entities | ||
} | ||
} | ||
// Defaults | ||
} else { | ||
return { | ||
...config, | ||
targets: {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.