Clean up Rachio binary sensor init (#34855)

* Update binary sensor

* Move online state to subclass
This commit is contained in:
Brian Rogers 2020-04-29 12:39:09 -04:00 committed by GitHub
parent f656f352a3
commit 481b2035db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 28 deletions

View file

@ -15,7 +15,6 @@ from .const import (
KEY_STATUS, KEY_STATUS,
KEY_SUBTYPE, KEY_SUBTYPE,
SIGNAL_RACHIO_CONTROLLER_UPDATE, SIGNAL_RACHIO_CONTROLLER_UPDATE,
STATUS_OFFLINE,
STATUS_ONLINE, STATUS_ONLINE,
) )
from .entity import RachioDevice from .entity import RachioDevice
@ -41,13 +40,10 @@ def _create_entities(hass, config_entry):
class RachioControllerBinarySensor(RachioDevice, BinarySensorEntity): class RachioControllerBinarySensor(RachioDevice, BinarySensorEntity):
"""Represent a binary sensor that reflects a Rachio state.""" """Represent a binary sensor that reflects a Rachio state."""
def __init__(self, controller, poll=True): def __init__(self, controller):
"""Set up a new Rachio controller binary sensor.""" """Set up a new Rachio controller binary sensor."""
super().__init__(controller) super().__init__(controller)
if poll: self._state = None
self._state = self._poll_update()
else:
self._state = None
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
@ -64,10 +60,6 @@ class RachioControllerBinarySensor(RachioDevice, BinarySensorEntity):
# For this device # For this device
self._async_handle_update(args, kwargs) self._async_handle_update(args, kwargs)
@abstractmethod
def _poll_update(self, data=None) -> bool:
"""Request the state from the API."""
@abstractmethod @abstractmethod
def _async_handle_update(self, *args, **kwargs) -> None: def _async_handle_update(self, *args, **kwargs) -> None:
"""Handle an update to the state of this sensor.""" """Handle an update to the state of this sensor."""
@ -86,11 +78,6 @@ class RachioControllerBinarySensor(RachioDevice, BinarySensorEntity):
class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor): class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
"""Represent a binary sensor that reflects if the controller is online.""" """Represent a binary sensor that reflects if the controller is online."""
def __init__(self, controller):
"""Set up a new Rachio controller online binary sensor."""
super().__init__(controller, poll=False)
self._state = self._poll_update(controller.init_data)
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of this sensor including the controller name.""" """Return the name of this sensor including the controller name."""
@ -111,18 +98,10 @@ class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
"""Return the name of an icon for this sensor.""" """Return the name of an icon for this sensor."""
return "mdi:wifi-strength-4" if self.is_on else "mdi:wifi-strength-off-outline" return "mdi:wifi-strength-4" if self.is_on else "mdi:wifi-strength-off-outline"
def _poll_update(self, data=None) -> bool: async def async_added_to_hass(self):
"""Request the state from the API.""" """Get initial state."""
if data is None: self._state = self._controller.init_data[KEY_STATUS] == STATUS_ONLINE
data = self._controller.rachio.device.get(self._controller.controller_id)[1] await super().async_added_to_hass()
if data[KEY_STATUS] == STATUS_ONLINE:
return True
if data[KEY_STATUS] == STATUS_OFFLINE:
return False
_LOGGER.warning(
'"%s" reported in unknown state "%s"', self.name, data[KEY_STATUS]
)
@callback @callback
def _async_handle_update(self, *args, **kwargs) -> None: def _async_handle_update(self, *args, **kwargs) -> None:

View file

@ -53,7 +53,6 @@ RACHIO_API_EXCEPTIONS = (
) )
STATUS_ONLINE = "ONLINE" STATUS_ONLINE = "ONLINE"
STATUS_OFFLINE = "OFFLINE"
SIGNAL_RACHIO_UPDATE = f"{DOMAIN}_update" SIGNAL_RACHIO_UPDATE = f"{DOMAIN}_update"
SIGNAL_RACHIO_CONTROLLER_UPDATE = f"{SIGNAL_RACHIO_UPDATE}_controller" SIGNAL_RACHIO_CONTROLLER_UPDATE = f"{SIGNAL_RACHIO_UPDATE}_controller"