Remove redundant property definitions in RainMachine (#52456)

* Remove redundant property definitions in RainMachine

* Incorrect attribute name
This commit is contained in:
Aaron Bach 2021-07-03 11:23:52 -05:00 committed by GitHub
parent 413c3afa12
commit 8c0559cc57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 145 deletions

View file

@ -53,6 +53,8 @@ CONF_PROGRAM_ID = "program_id"
CONF_SECONDS = "seconds"
CONF_ZONE_ID = "zone_id"
DEFAULT_ICON = "mdi:water"
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
RUN_STATUS_MAP = {0: "Not Running", 1: "Running", 2: "Queued"}
@ -181,6 +183,8 @@ async def async_setup_entry(
class RainMachineSwitch(RainMachineEntity, SwitchEntity):
"""A class to represent a generic RainMachine switch."""
_attr_icon = DEFAULT_ICON
def __init__(
self,
coordinator: DataUpdateCoordinator,
@ -190,34 +194,24 @@ class RainMachineSwitch(RainMachineEntity, SwitchEntity):
entry: ConfigEntry,
) -> None:
"""Initialize a generic RainMachine switch."""
super().__init__(coordinator, controller)
super().__init__(coordinator, controller, type(self).__name__)
self._attr_is_on = False
self._attr_name = name
self._data = coordinator.data[uid]
self._entry = entry
self._is_active = True
self._is_on = False
self._name = name
self._switch_type = type(self).__name__
self._uid = uid
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._is_active and self.coordinator.last_update_success
@property
def icon(self) -> str:
"""Return the icon."""
return "mdi:water"
@property
def is_on(self) -> bool:
"""Return whether the program is running."""
return self._is_on
return super().available and self._is_active
@property
def unique_id(self) -> str:
"""Return a unique, Home Assistant friendly identifier for this entity."""
return f"{self._unique_id}_{self._switch_type}_{self._uid}"
return f"{super().unique_id}_{self._uid}"
async def _async_run_switch_coroutine(self, api_coro: Coroutine) -> None:
"""Run a coroutine to toggle the switch."""
@ -226,7 +220,7 @@ class RainMachineSwitch(RainMachineEntity, SwitchEntity):
except RequestError as err:
LOGGER.error(
'Error while toggling %s "%s": %s',
self._switch_type,
self._entity_type,
self.unique_id,
err,
)
@ -235,7 +229,7 @@ class RainMachineSwitch(RainMachineEntity, SwitchEntity):
if resp["statusCode"] != 0:
LOGGER.error(
'Error while toggling %s "%s": %s',
self._switch_type,
self._entity_type,
self.unique_id,
resp["message"],
)
@ -334,7 +328,7 @@ class RainMachineProgram(RainMachineSwitch):
"""Update the state."""
super().update_from_latest_data()
self._is_on = bool(self._data["status"])
self._attr_is_on = bool(self._data["status"])
if self._data.get("nextRun") is not None:
next_run = datetime.strptime(
@ -344,7 +338,7 @@ class RainMachineProgram(RainMachineSwitch):
else:
next_run = None
self._attrs.update(
self._attr_extra_state_attributes.update(
{
ATTR_ID: self._uid,
ATTR_NEXT_RUN: next_run,
@ -376,9 +370,9 @@ class RainMachineZone(RainMachineSwitch):
"""Update the state."""
super().update_from_latest_data()
self._is_on = bool(self._data["state"])
self._attr_is_on = bool(self._data["state"])
self._attrs.update(
self._attr_extra_state_attributes.update(
{
ATTR_STATUS: RUN_STATUS_MAP[self._data["state"]],
ATTR_AREA: self._data.get("waterSense").get("area"),