switch.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """Switch setup for our Integration."""
  2. import logging
  3. from typing import Any
  4. from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
  5. from homeassistant.core import HomeAssistant
  6. from homeassistant.helpers.entity_platform import AddEntitiesCallback
  7. from . import MyConfigEntry
  8. from .base import ExampleBaseEntity
  9. from .coordinator import ExampleCoordinator
  10. _LOGGER = logging.getLogger(__name__)
  11. async def async_setup_entry(
  12. hass: HomeAssistant,
  13. config_entry: MyConfigEntry,
  14. async_add_entities: AddEntitiesCallback,
  15. ):
  16. """Set up the Binary Sensors."""
  17. # This gets the data update coordinator from the config entry runtime data as specified in your __init__.py
  18. coordinator: ExampleCoordinator = config_entry.runtime_data.coordinator
  19. # ----------------------------------------------------------------------------
  20. # Here we enumerate the switches in your data value from your
  21. # DataUpdateCoordinator and add an instance of your switch class to a list
  22. # for each one.
  23. # This maybe different in your specific case, depending on how your data is
  24. # structured
  25. # ----------------------------------------------------------------------------
  26. switches = [
  27. ExampleSwitch(coordinator, device, "state")
  28. for device in coordinator.data
  29. if device.get("device_type") == "SOCKET"
  30. ]
  31. # Create the binary sensors.
  32. async_add_entities(switches)
  33. class ExampleSwitch(ExampleBaseEntity, SwitchEntity):
  34. """Implementation of a switch.
  35. This inherits our ExampleBaseEntity to set common properties.
  36. See base.py for this class.
  37. https://developers.home-assistant.io/docs/core/entity/switch
  38. """
  39. _attr_device_class = SwitchDeviceClass.SWITCH
  40. @property
  41. def is_on(self) -> bool | None:
  42. """Return if the binary sensor is on."""
  43. # This needs to enumerate to true or false
  44. return (
  45. self.coordinator.get_device_parameter(self.device_id, self.parameter)
  46. == "ON"
  47. )
  48. async def async_turn_on(self, **kwargs: Any) -> None:
  49. """Turn the entity on."""
  50. await self.hass.async_add_executor_job(
  51. self.coordinator.api.set_data, self.device_id, self.parameter, "ON"
  52. )
  53. # ----------------------------------------------------------------------------
  54. # Use async_refresh on the DataUpdateCoordinator to perform immediate update.
  55. # Using self.async_update or self.coordinator.async_request_refresh may delay update due
  56. # to trying to batch requests.
  57. # ----------------------------------------------------------------------------
  58. await self.coordinator.async_refresh()
  59. async def async_turn_off(self, **kwargs: Any) -> None:
  60. """Turn the entity off."""
  61. await self.hass.async_add_executor_job(
  62. self.coordinator.api.set_data, self.device_id, self.parameter, "OFF"
  63. )
  64. # ----------------------------------------------------------------------------
  65. # Use async_refresh on the DataUpdateCoordinator to perform immediate update.
  66. # Using self.async_update or self.coordinator.async_request_refresh may delay update due
  67. # to trying to batch requests.
  68. # ----------------------------------------------------------------------------
  69. await self.coordinator.async_refresh()
  70. @property
  71. def extra_state_attributes(self):
  72. """Return the extra state attributes."""
  73. # Add any additional attributes you want on your sensor.
  74. attrs = {}
  75. attrs["last_rebooted"] = self.coordinator.get_device_parameter(
  76. self.device_id, "last_reboot"
  77. )
  78. return attrs