Remove redundant property definitions in Guardian (#52361)
* Remove redundant property definitions in Guardian * Update incorrect attributes
This commit is contained in:
parent
9d04ba2804
commit
6fa312d476
4 changed files with 67 additions and 164 deletions
|
@ -9,7 +9,6 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.const import ATTR_ATTRIBUTION, CONF_IP_ADDRESS, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
DataUpdateCoordinator,
|
||||
|
@ -218,34 +217,12 @@ class GuardianEntity(CoordinatorEntity):
|
|||
self, entry: ConfigEntry, kind: str, name: str, device_class: str, icon: str
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
self._attrs = {ATTR_ATTRIBUTION: "Data provided by Elexa"}
|
||||
self._available = True
|
||||
self._attr_device_class = device_class
|
||||
self._attr_device_info = {"manufacturer": "Elexa"}
|
||||
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: "Data provided by Elexa"}
|
||||
self._attr_icon = icon
|
||||
self._attr_name = name
|
||||
self._entry = entry
|
||||
self._device_class = device_class
|
||||
self._device_info = {"manufacturer": "Elexa"}
|
||||
self._icon = icon
|
||||
self._kind = kind
|
||||
self._name = name
|
||||
|
||||
@property
|
||||
def device_class(self) -> str:
|
||||
"""Return the device class."""
|
||||
return self._device_class
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device registry information for this entity."""
|
||||
return self._device_info
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict:
|
||||
"""Return the state attributes."""
|
||||
return self._attrs
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the icon."""
|
||||
return self._icon
|
||||
|
||||
@callback
|
||||
def _async_update_from_latest_data(self):
|
||||
|
@ -255,12 +232,6 @@ class GuardianEntity(CoordinatorEntity):
|
|||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@callback
|
||||
def _async_update_state_callback(self):
|
||||
"""Update the entity's state."""
|
||||
self._async_update_from_latest_data()
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class PairedSensorEntity(GuardianEntity):
|
||||
"""Define a Guardian paired sensor entity."""
|
||||
|
@ -277,24 +248,15 @@ class PairedSensorEntity(GuardianEntity):
|
|||
"""Initialize."""
|
||||
super().__init__(entry, kind, name, device_class, icon)
|
||||
|
||||
paired_sensor_uid = coordinator.data["uid"]
|
||||
self._attr_device_info["identifiers"] = {(DOMAIN, paired_sensor_uid)}
|
||||
self._attr_device_info["name"] = f"Guardian Paired Sensor {paired_sensor_uid}"
|
||||
self._attr_device_info["via_device"] = (DOMAIN, entry.data[CONF_UID])
|
||||
self._attr_name = f"Guardian Paired Sensor {paired_sensor_uid}: {name}"
|
||||
self._attr_unique_id = f"{paired_sensor_uid}_{kind}"
|
||||
self._kind = kind
|
||||
self.coordinator = coordinator
|
||||
|
||||
self._paired_sensor_uid = coordinator.data["uid"]
|
||||
|
||||
self._device_info["identifiers"] = {(DOMAIN, self._paired_sensor_uid)}
|
||||
self._device_info["name"] = f"Guardian Paired Sensor {self._paired_sensor_uid}"
|
||||
self._device_info["via_device"] = (DOMAIN, self._entry.data[CONF_UID])
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the entity."""
|
||||
return f"Guardian Paired Sensor {self._paired_sensor_uid}: {self._name}"
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the unique ID of the entity."""
|
||||
return f"{self._paired_sensor_uid}_{self._kind}"
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Perform tasks when the entity is added."""
|
||||
self._async_update_from_latest_data()
|
||||
|
@ -315,30 +277,26 @@ class ValveControllerEntity(GuardianEntity):
|
|||
"""Initialize."""
|
||||
super().__init__(entry, kind, name, device_class, icon)
|
||||
|
||||
self.coordinators = coordinators
|
||||
|
||||
self._device_info["identifiers"] = {(DOMAIN, self._entry.data[CONF_UID])}
|
||||
self._device_info[
|
||||
self._attr_device_info["identifiers"] = {(DOMAIN, entry.data[CONF_UID])}
|
||||
self._attr_device_info[
|
||||
"name"
|
||||
] = f"Guardian Valve Controller {self._entry.data[CONF_UID]}"
|
||||
self._device_info["model"] = self.coordinators[API_SYSTEM_DIAGNOSTICS].data[
|
||||
] = f"Guardian Valve Controller {entry.data[CONF_UID]}"
|
||||
self._attr_device_info["model"] = coordinators[API_SYSTEM_DIAGNOSTICS].data[
|
||||
"firmware"
|
||||
]
|
||||
self._attr_name = f"Guardian {entry.data[CONF_UID]}: {name}"
|
||||
self._attr_unique_id = f"{entry.data[CONF_UID]}_{kind}"
|
||||
self._kind = kind
|
||||
self.coordinators = coordinators
|
||||
|
||||
@property
|
||||
def availabile(self) -> bool:
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
return any(coordinator.last_update_success for coordinator in self.coordinators)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the entity."""
|
||||
return f"Guardian {self._entry.data[CONF_UID]}: {self._name}"
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the unique ID of the entity."""
|
||||
return f"{self._entry.data[CONF_UID]}_{self._kind}"
|
||||
return any(
|
||||
coordinator.last_update_success
|
||||
for coordinator in self.coordinators.values()
|
||||
if coordinator
|
||||
)
|
||||
|
||||
async def _async_continue_entity_setup(self):
|
||||
"""Perform additional, internal tasks when the entity is about to be added.
|
||||
|
@ -350,9 +308,14 @@ class ValveControllerEntity(GuardianEntity):
|
|||
@callback
|
||||
def async_add_coordinator_update_listener(self, api: str) -> None:
|
||||
"""Add a listener to a DataUpdateCoordinator based on the API referenced."""
|
||||
self.async_on_remove(
|
||||
self.coordinators[api].async_add_listener(self._async_update_state_callback)
|
||||
)
|
||||
|
||||
@callback
|
||||
def update():
|
||||
"""Update the entity's state."""
|
||||
self._async_update_from_latest_data()
|
||||
self.async_write_ha_state()
|
||||
|
||||
self.async_on_remove(self.coordinators[api].async_add_listener(update))
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Perform tasks when the entity is added."""
|
||||
|
@ -365,7 +328,6 @@ class ValveControllerEntity(GuardianEntity):
|
|||
|
||||
Only used by the generic entity update service.
|
||||
"""
|
||||
|
||||
# Ignore manual update requests if the entity is disabled
|
||||
if not self.enabled:
|
||||
return
|
||||
|
|
|
@ -129,25 +129,15 @@ class PairedSensorBinarySensor(PairedSensorEntity, BinarySensorEntity):
|
|||
"""Initialize."""
|
||||
super().__init__(entry, coordinator, kind, name, device_class, icon)
|
||||
|
||||
self._is_on = True
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return whether the entity is available."""
|
||||
return self.coordinator.last_update_success
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return True if the binary sensor is on."""
|
||||
return self._is_on
|
||||
self._attr_is_on = True
|
||||
|
||||
@callback
|
||||
def _async_update_from_latest_data(self) -> None:
|
||||
"""Update the entity."""
|
||||
if self._kind == SENSOR_KIND_LEAK_DETECTED:
|
||||
self._is_on = self.coordinator.data["wet"]
|
||||
self._attr_is_on = self.coordinator.data["wet"]
|
||||
elif self._kind == SENSOR_KIND_MOVED:
|
||||
self._is_on = self.coordinator.data["moved"]
|
||||
self._attr_is_on = self.coordinator.data["moved"]
|
||||
|
||||
|
||||
class ValveControllerBinarySensor(ValveControllerEntity, BinarySensorEntity):
|
||||
|
@ -165,23 +155,7 @@ class ValveControllerBinarySensor(ValveControllerEntity, BinarySensorEntity):
|
|||
"""Initialize."""
|
||||
super().__init__(entry, coordinators, kind, name, device_class, icon)
|
||||
|
||||
self._is_on = True
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return whether the entity is available."""
|
||||
if self._kind == SENSOR_KIND_AP_INFO:
|
||||
return self.coordinators[API_WIFI_STATUS].last_update_success
|
||||
if self._kind == SENSOR_KIND_LEAK_DETECTED:
|
||||
return self.coordinators[
|
||||
API_SYSTEM_ONBOARD_SENSOR_STATUS
|
||||
].last_update_success
|
||||
return False
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return True if the binary sensor is on."""
|
||||
return self._is_on
|
||||
self._attr_is_on = True
|
||||
|
||||
async def _async_continue_entity_setup(self) -> None:
|
||||
"""Add an API listener."""
|
||||
|
@ -194,8 +168,13 @@ class ValveControllerBinarySensor(ValveControllerEntity, BinarySensorEntity):
|
|||
def _async_update_from_latest_data(self) -> None:
|
||||
"""Update the entity."""
|
||||
if self._kind == SENSOR_KIND_AP_INFO:
|
||||
self._is_on = self.coordinators[API_WIFI_STATUS].data["station_connected"]
|
||||
self._attrs.update(
|
||||
self._attr_available = self.coordinators[
|
||||
API_WIFI_STATUS
|
||||
].last_update_success
|
||||
self._attr_is_on = self.coordinators[API_WIFI_STATUS].data[
|
||||
"station_connected"
|
||||
]
|
||||
self._attr_extra_state_attributes.update(
|
||||
{
|
||||
ATTR_CONNECTED_CLIENTS: self.coordinators[API_WIFI_STATUS].data.get(
|
||||
"ap_clients"
|
||||
|
@ -203,6 +182,9 @@ class ValveControllerBinarySensor(ValveControllerEntity, BinarySensorEntity):
|
|||
}
|
||||
)
|
||||
elif self._kind == SENSOR_KIND_LEAK_DETECTED:
|
||||
self._is_on = self.coordinators[API_SYSTEM_ONBOARD_SENSOR_STATUS].data[
|
||||
self._attr_available = self.coordinators[
|
||||
API_SYSTEM_ONBOARD_SENSOR_STATUS
|
||||
].last_update_success
|
||||
self._attr_is_on = self.coordinators[API_SYSTEM_ONBOARD_SENSOR_STATUS].data[
|
||||
"wet"
|
||||
]
|
||||
|
|
|
@ -126,31 +126,15 @@ class PairedSensorSensor(PairedSensorEntity, SensorEntity):
|
|||
"""Initialize."""
|
||||
super().__init__(entry, coordinator, kind, name, device_class, icon)
|
||||
|
||||
self._state = None
|
||||
self._unit = unit
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return whether the entity is available."""
|
||||
return self.coordinator.last_update_success
|
||||
|
||||
@property
|
||||
def state(self) -> str:
|
||||
"""Return the sensor state."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
return self._unit
|
||||
self._attr_unit_of_measurement = unit
|
||||
|
||||
@callback
|
||||
def _async_update_from_latest_data(self) -> None:
|
||||
"""Update the entity."""
|
||||
if self._kind == SENSOR_KIND_BATTERY:
|
||||
self._state = self.coordinator.data["battery"]
|
||||
self._attr_state = self.coordinator.data["battery"]
|
||||
elif self._kind == SENSOR_KIND_TEMPERATURE:
|
||||
self._state = self.coordinator.data["temperature"]
|
||||
self._attr_state = self.coordinator.data["temperature"]
|
||||
|
||||
|
||||
class ValveControllerSensor(ValveControllerEntity, SensorEntity):
|
||||
|
@ -169,29 +153,7 @@ class ValveControllerSensor(ValveControllerEntity, SensorEntity):
|
|||
"""Initialize."""
|
||||
super().__init__(entry, coordinators, kind, name, device_class, icon)
|
||||
|
||||
self._state = None
|
||||
self._unit = unit
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return whether the entity is available."""
|
||||
if self._kind == SENSOR_KIND_TEMPERATURE:
|
||||
return self.coordinators[
|
||||
API_SYSTEM_ONBOARD_SENSOR_STATUS
|
||||
].last_update_success
|
||||
if self._kind == SENSOR_KIND_UPTIME:
|
||||
return self.coordinators[API_SYSTEM_DIAGNOSTICS].last_update_success
|
||||
return False
|
||||
|
||||
@property
|
||||
def state(self) -> str:
|
||||
"""Return the sensor state."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
return self._unit
|
||||
self._attr_unit_of_measurement = unit
|
||||
|
||||
async def _async_continue_entity_setup(self) -> None:
|
||||
"""Register API interest (and related tasks) when the entity is added."""
|
||||
|
@ -202,8 +164,14 @@ class ValveControllerSensor(ValveControllerEntity, SensorEntity):
|
|||
def _async_update_from_latest_data(self) -> None:
|
||||
"""Update the entity."""
|
||||
if self._kind == SENSOR_KIND_TEMPERATURE:
|
||||
self._state = self.coordinators[API_SYSTEM_ONBOARD_SENSOR_STATUS].data[
|
||||
self._attr_available = self.coordinators[
|
||||
API_SYSTEM_ONBOARD_SENSOR_STATUS
|
||||
].last_update_success
|
||||
self._attr_state = self.coordinators[API_SYSTEM_ONBOARD_SENSOR_STATUS].data[
|
||||
"temperature"
|
||||
]
|
||||
elif self._kind == SENSOR_KIND_UPTIME:
|
||||
self._state = self.coordinators[API_SYSTEM_DIAGNOSTICS].data["uptime"]
|
||||
self._attr_available = self.coordinators[
|
||||
API_SYSTEM_DIAGNOSTICS
|
||||
].last_update_success
|
||||
self._attr_state = self.coordinators[API_SYSTEM_DIAGNOSTICS].data["uptime"]
|
||||
|
|
|
@ -92,18 +92,8 @@ class ValveControllerSwitch(ValveControllerEntity, SwitchEntity):
|
|||
entry, coordinators, "valve", "Valve Controller", None, "mdi:water"
|
||||
)
|
||||
|
||||
self._attr_is_on = True
|
||||
self._client = client
|
||||
self._is_on = True
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return whether the entity is available."""
|
||||
return self.coordinators[API_VALVE_STATUS].last_update_success
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return True if the valve is open."""
|
||||
return self._is_on
|
||||
|
||||
async def _async_continue_entity_setup(self):
|
||||
"""Register API interest (and related tasks) when the entity is added."""
|
||||
|
@ -112,14 +102,15 @@ class ValveControllerSwitch(ValveControllerEntity, SwitchEntity):
|
|||
@callback
|
||||
def _async_update_from_latest_data(self) -> None:
|
||||
"""Update the entity."""
|
||||
self._is_on = self.coordinators[API_VALVE_STATUS].data["state"] in (
|
||||
self._attr_available = self.coordinators[API_VALVE_STATUS].last_update_success
|
||||
self._attr_is_on = self.coordinators[API_VALVE_STATUS].data["state"] in (
|
||||
"start_opening",
|
||||
"opening",
|
||||
"finish_opening",
|
||||
"opened",
|
||||
)
|
||||
|
||||
self._attrs.update(
|
||||
self._attr_extra_state_attributes.update(
|
||||
{
|
||||
ATTR_AVG_CURRENT: self.coordinators[API_VALVE_STATUS].data[
|
||||
"average_current"
|
||||
|
@ -215,7 +206,7 @@ class ValveControllerSwitch(ValveControllerEntity, SwitchEntity):
|
|||
LOGGER.error("Error while closing the valve: %s", err)
|
||||
return
|
||||
|
||||
self._is_on = False
|
||||
self._attr_is_on = False
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_turn_on(self, **kwargs) -> None:
|
||||
|
@ -227,5 +218,5 @@ class ValveControllerSwitch(ValveControllerEntity, SwitchEntity):
|
|||
LOGGER.error("Error while opening the valve: %s", err)
|
||||
return
|
||||
|
||||
self._is_on = True
|
||||
self._attr_is_on = True
|
||||
self.async_write_ha_state()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue