binary_sensor.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """Support for Gardena Smart System websocket connection status."""
  2. from homeassistant.components.binary_sensor import (
  3. DEVICE_CLASS_CONNECTIVITY,
  4. BinarySensorEntity,
  5. )
  6. from custom_components.gardena_smart_system import GARDENA_SYSTEM
  7. from .const import DOMAIN
  8. async def async_setup_entry(hass, config_entry, async_add_entities):
  9. """Perform the setup for Gardena websocket connection status."""
  10. async_add_entities([SmartSystemWebsocketStatus(hass.data[DOMAIN][GARDENA_SYSTEM].smart_system)], True)
  11. class SmartSystemWebsocketStatus(BinarySensorEntity):
  12. """Representation of Gardena Smart System websocket connection status."""
  13. def __init__(self, smart_system) -> None:
  14. """Initialize the binary sensor."""
  15. super().__init__()
  16. self._unique_id = "smart_gardena_websocket_status"
  17. self._name = "Gardena Smart System connection"
  18. self._smart_system = smart_system
  19. async def async_added_to_hass(self):
  20. """Subscribe to events."""
  21. self._smart_system.add_ws_status_callback(self.update_callback)
  22. @property
  23. def name(self):
  24. """Return the name of the device."""
  25. return self._name
  26. @property
  27. def unique_id(self) -> str:
  28. """Return a unique ID."""
  29. return self._unique_id
  30. @property
  31. def is_on(self) -> bool:
  32. """Return the status of the sensor."""
  33. return self._smart_system.is_ws_connected
  34. @property
  35. def should_poll(self) -> bool:
  36. """No polling needed for a sensor."""
  37. return False
  38. def update_callback(self, status):
  39. """Call update for Home Assistant when the device is updated."""
  40. self.schedule_update_ha_state(True)
  41. @property
  42. def device_class(self):
  43. """Return the class of this device, from component DEVICE_CLASSES."""
  44. return DEVICE_CLASS_CONNECTIVITY