From 174cc23309e84477350eff051d545620bd3c127a Mon Sep 17 00:00:00 2001 From: Scott Colby Date: Mon, 9 Jan 2023 10:41:17 -0500 Subject: [PATCH] Add "Schedule Part" enum sensor to Venstar thermostat (#84332) --- homeassistant/components/venstar/sensor.py | 73 ++++++++++++++----- homeassistant/components/venstar/strings.json | 13 ++++ .../components/venstar/translations/en.json | 13 ++++ 3 files changed, 79 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/venstar/sensor.py b/homeassistant/components/venstar/sensor.py index 0d3b66c620e..e20c748f112 100644 --- a/homeassistant/components/venstar/sensor.py +++ b/homeassistant/components/venstar/sensor.py @@ -56,6 +56,14 @@ RUNTIME_ATTRIBUTES = { RUNTIME_OV: "Override", } +SCHEDULE_PARTS: dict[int, str] = { + 0: "morning", + 1: "day", + 2: "evening", + 3: "night", + 255: "inactive", +} + @dataclass class VenstarSensorTypeMixin: @@ -76,31 +84,42 @@ async def async_setup_entry( config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: - """Set up Vensar device binary_sensors based on a config entry.""" + """Set up Venstar device sensors based on a config entry.""" coordinator: VenstarDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] entities: list[Entity] = [] - if not (sensors := coordinator.client.get_sensor_list()): - return - - for sensor_name in sensors: - entities.extend( - [ - VenstarSensor(coordinator, config_entry, description, sensor_name) - for description in SENSOR_ENTITIES - if coordinator.client.get_sensor(sensor_name, description.key) - is not None - ] - ) - - runtimes = coordinator.runtimes[-1] - for sensor_name in runtimes: - if sensor_name in RUNTIME_DEVICES: - entities.append( - VenstarSensor(coordinator, config_entry, RUNTIME_ENTITY, sensor_name) + if sensors := coordinator.client.get_sensor_list(): + for sensor_name in sensors: + entities.extend( + [ + VenstarSensor(coordinator, config_entry, description, sensor_name) + for description in SENSOR_ENTITIES + if coordinator.client.get_sensor(sensor_name, description.key) + is not None + ] ) - async_add_entities(entities) + runtimes = coordinator.runtimes[-1] + for sensor_name in runtimes: + if sensor_name in RUNTIME_DEVICES: + entities.append( + VenstarSensor( + coordinator, config_entry, RUNTIME_ENTITY, sensor_name + ) + ) + + for description in INFO_ENTITIES: + try: + # just checking if the key exists + coordinator.client.get_info(description.key) + except KeyError: + continue + entities.append( + VenstarSensor(coordinator, config_entry, description, description.key) + ) + + if entities: + async_add_entities(entities) def temperature_unit(coordinator: VenstarDataUpdateCoordinator) -> str: @@ -210,3 +229,17 @@ RUNTIME_ENTITY = VenstarSensorEntityDescription( value_fn=lambda coordinator, sensor_name: coordinator.runtimes[-1][sensor_name], name_fn=lambda coordinator, sensor_name: f"{coordinator.client.name} {RUNTIME_ATTRIBUTES[sensor_name]} Runtime", ) + +INFO_ENTITIES: tuple[VenstarSensorEntityDescription, ...] = ( + VenstarSensorEntityDescription( + key="schedulepart", + device_class=SensorDeviceClass.ENUM, + options=list(SCHEDULE_PARTS.values()), + translation_key="schedule_part", + uom_fn=lambda _: None, + value_fn=lambda coordinator, sensor_name: SCHEDULE_PARTS[ + coordinator.client.get_info(sensor_name) + ], + name_fn=lambda coordinator, sensor_name: f"{coordinator.client.name} Schedule Part", + ), +) diff --git a/homeassistant/components/venstar/strings.json b/homeassistant/components/venstar/strings.json index 9b031d94188..a844adc2156 100644 --- a/homeassistant/components/venstar/strings.json +++ b/homeassistant/components/venstar/strings.json @@ -19,5 +19,18 @@ "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "unknown": "[%key:common::config_flow::error::unknown%]" } + }, + "entity": { + "sensor": { + "schedule_part": { + "state": { + "morning": "Morning", + "day": "Day", + "evening": "Evening", + "night": "Night", + "inactive": "Inactive" + } + } + } } } diff --git a/homeassistant/components/venstar/translations/en.json b/homeassistant/components/venstar/translations/en.json index 8b423713f2c..107f5fd3e91 100644 --- a/homeassistant/components/venstar/translations/en.json +++ b/homeassistant/components/venstar/translations/en.json @@ -19,5 +19,18 @@ "title": "Connect to the Venstar Thermostat" } } + }, + "entity": { + "sensor": { + "schedule_part": { + "state": { + "day": "Day", + "evening": "Evening", + "inactive": "Inactive", + "morning": "Morning", + "night": "Night" + } + } + } } } \ No newline at end of file