text.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from homeassistant.core import HomeAssistant
  2. from homeassistant.const import EntityCategory
  3. from homeassistant.helpers.entity import Entity
  4. from homeassistant.components.text import TextEntity
  5. from homeassistant.helpers.entity_platform import AddEntitiesCallback
  6. from . import ZiBConfigEntry
  7. from .entity import ZiBEntityBase
  8. from .const import ROOM_STATE_TEXTS
  9. async def async_setup_entry(
  10. hass: HomeAssistant,
  11. config_entry: ZiBConfigEntry,
  12. async_add_entities: AddEntitiesCallback,
  13. ) -> None:
  14. state = config_entry.runtime_data.state
  15. async_add_entities([
  16. Reservation_Defaults(state),
  17. ])
  18. class ZiBTextBase(ZiBEntityBase, TextEntity):
  19. _attr_entity_category = EntityCategory.CONFIG
  20. def __init__(self, entity: Entity, name: str, icon: str = None):
  21. self._field = name.lower().replace(' ', '_')
  22. if self._field not in ROOM_STATE_TEXTS:
  23. raise KeyError(self._field)
  24. super().__init__(entity, name, f"{entity.id}_{self._field}", icon)
  25. # Fake in some values for testing
  26. for field in ROOM_STATE_TEXTS:
  27. self._entity.set(field, False)
  28. @property
  29. def state(self) -> str:
  30. return self._entity._data[self._field]
  31. async def async_set_value(self, value: str) -> None:
  32. field = self.name.lower().replace(' ', '_')
  33. entity = self.platform.config_entry.runtime_data.state
  34. entity.set(field, value)
  35. await entity.publish_updates()
  36. class Reservation_Defaults(ZiBTextBase):
  37. def __init__(self, state: RoomState):
  38. super().__init__(state, 'Reservation Defaults', 'mdi:format-list-group')