Add entity translations to Rachio (#98917)

This commit is contained in:
Joost Lekkerkerker 2023-08-24 11:43:10 +02:00 committed by GitHub
parent c47983621c
commit 577f545113
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 55 deletions

View file

@ -43,7 +43,6 @@ async def async_setup_entry(
"""Set up the Rachio binary sensors."""
entities = await hass.async_add_executor_job(_create_entities, hass, config_entry)
async_add_entities(entities)
_LOGGER.debug("%d Rachio binary sensor(s) added", len(entities))
def _create_entities(hass: HomeAssistant, config_entry: ConfigEntry) -> list[Entity]:
@ -58,6 +57,8 @@ def _create_entities(hass: HomeAssistant, config_entry: ConfigEntry) -> list[Ent
class RachioControllerBinarySensor(RachioDevice, BinarySensorEntity):
"""Represent a binary sensor that reflects a Rachio state."""
_attr_has_entity_name = True
def __init__(self, controller):
"""Set up a new Rachio controller binary sensor."""
super().__init__(controller)
@ -86,26 +87,13 @@ class RachioControllerBinarySensor(RachioDevice, BinarySensorEntity):
class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
"""Represent a binary sensor that reflects if the controller is online."""
@property
def name(self) -> str:
"""Return the name of this sensor including the controller name."""
return self._controller.name
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
@property
def unique_id(self) -> str:
"""Return a unique id for this entity."""
return f"{self._controller.controller_id}-online"
@property
def device_class(self) -> BinarySensorDeviceClass:
"""Return the class of this device, from BinarySensorDeviceClass."""
return BinarySensorDeviceClass.CONNECTIVITY
@property
def icon(self) -> str:
"""Return the name of an icon for this sensor."""
return "mdi:wifi-strength-4" if self.is_on else "mdi:wifi-strength-off-outline"
@callback
def _async_handle_update(self, *args, **kwargs) -> None:
"""Handle an update to the state of this sensor."""
@ -132,26 +120,14 @@ class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
class RachioRainSensor(RachioControllerBinarySensor):
"""Represent a binary sensor that reflects the status of the rain sensor."""
@property
def name(self) -> str:
"""Return the name of this sensor including the controller name."""
return f"{self._controller.name} rain sensor"
_attr_device_class = BinarySensorDeviceClass.MOISTURE
_attr_translation_key = "rain"
@property
def unique_id(self) -> str:
"""Return a unique id for this entity."""
return f"{self._controller.controller_id}-rain_sensor"
@property
def device_class(self) -> BinarySensorDeviceClass:
"""Return the class of this device."""
return BinarySensorDeviceClass.MOISTURE
@property
def icon(self) -> str:
"""Return the icon for this sensor."""
return "mdi:water" if self.is_on else "mdi:water-off"
@callback
def _async_handle_update(self, *args, **kwargs) -> None:
"""Handle an update to the state of this sensor."""

View file

@ -27,6 +27,21 @@
}
}
},
"entity": {
"binary_sensor": {
"rain": {
"name": "Rain"
}
},
"switch": {
"standby": {
"name": "Standby"
},
"rain_delay": {
"name": "Rain delay"
}
}
},
"services": {
"set_zone_moisture_percent": {
"name": "Set zone moisture percent",

View file

@ -109,7 +109,6 @@ async def async_setup_entry(
has_flex_sched = True
async_add_entities(entities)
_LOGGER.debug("%d Rachio switch(es) added", len(entities))
def start_multiple(service: ServiceCall) -> None:
"""Service to start multiple zones in sequence."""
@ -173,7 +172,6 @@ def _create_entities(hass: HomeAssistant, config_entry: ConfigEntry) -> list[Ent
entities.append(RachioZone(person, controller, zone, current_schedule))
for sched in schedules + flex_schedules:
entities.append(RachioSchedule(person, controller, sched, current_schedule))
_LOGGER.debug("Added %s", entities)
return entities
@ -185,11 +183,6 @@ class RachioSwitch(RachioDevice, SwitchEntity):
super().__init__(controller)
self._state = None
@property
def name(self) -> str:
"""Get a name for this switch."""
return f"Switch on {self._controller.name}"
@property
def is_on(self) -> bool:
"""Return whether the switch is currently on."""
@ -213,21 +206,15 @@ class RachioSwitch(RachioDevice, SwitchEntity):
class RachioStandbySwitch(RachioSwitch):
"""Representation of a standby status/button."""
@property
def name(self) -> str:
"""Return the name of the standby switch."""
return f"{self._controller.name} in standby mode"
_attr_has_entity_name = True
_attr_translation_key = "standby"
_attr_icon = "mdi:power"
@property
def unique_id(self) -> str:
"""Return a unique id by combining controller id and purpose."""
return f"{self._controller.controller_id}-standby"
@property
def icon(self) -> str:
"""Return an icon for the standby switch."""
return "mdi:power"
@callback
def _async_handle_update(self, *args, **kwargs) -> None:
"""Update the state using webhook data."""
@ -263,26 +250,20 @@ class RachioStandbySwitch(RachioSwitch):
class RachioRainDelay(RachioSwitch):
"""Representation of a rain delay status/switch."""
_attr_has_entity_name = True
_attr_translation_key = "rain_delay"
_attr_icon = "mdi:camera-timer"
def __init__(self, controller):
"""Set up a Rachio rain delay switch."""
self._cancel_update = None
super().__init__(controller)
@property
def name(self) -> str:
"""Return the name of the switch."""
return f"{self._controller.name} rain delay"
@property
def unique_id(self) -> str:
"""Return a unique id by combining controller id and purpose."""
return f"{self._controller.controller_id}-delay"
@property
def icon(self) -> str:
"""Return an icon for rain delay."""
return "mdi:camera-timer"
@callback
def _async_handle_update(self, *args, **kwargs) -> None:
"""Update the state using webhook data."""