Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """WiZ integration entities."""
2 
3 from __future__ import annotations
4 
5 from abc import abstractmethod
6 from typing import Any
7 
8 from pywizlight.bulblibrary import BulbType
9 
10 from homeassistant.const import ATTR_HW_VERSION, ATTR_MODEL
11 from homeassistant.core import callback
12 from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
13 from homeassistant.helpers.entity import Entity, ToggleEntity
15  CoordinatorEntity,
16  DataUpdateCoordinator,
17 )
18 
19 from .models import WizData
20 
21 
22 class WizEntity(CoordinatorEntity[DataUpdateCoordinator[float | None]], Entity):
23  """Representation of WiZ entity."""
24 
25  _attr_has_entity_name = True
26 
27  def __init__(self, wiz_data: WizData, name: str) -> None:
28  """Initialize a WiZ entity."""
29  super().__init__(wiz_data.coordinator)
30  self._device_device = wiz_data.bulb
31  bulb_type: BulbType = self._device_device.bulbtype
32  self._attr_unique_id_attr_unique_id = self._device_device.mac
33  self._attr_device_info_attr_device_info = DeviceInfo(
34  connections={(CONNECTION_NETWORK_MAC, self._device_device.mac)},
35  name=name,
36  manufacturer="WiZ",
37  sw_version=bulb_type.fw_version,
38  )
39  if bulb_type.name is None:
40  return
41  hw_data = bulb_type.name.split("_")
42  board = hw_data.pop(0)
43  model = hw_data.pop(0)
44  hw_version = f"{board} {hw_data[0]}" if hw_data else board
45  self._attr_device_info_attr_device_info[ATTR_HW_VERSION] = hw_version
46  self._attr_device_info_attr_device_info[ATTR_MODEL] = model
47 
48  @callback
49  def _handle_coordinator_update(self) -> None:
50  """Handle updated data from the coordinator."""
51  self._async_update_attrs_async_update_attrs()
53 
54  @callback
55  @abstractmethod
56  def _async_update_attrs(self) -> None:
57  """Handle updating _attr values."""
58 
59 
60 class WizToggleEntity(WizEntity, ToggleEntity):
61  """Representation of WiZ toggle entity."""
62 
63  @callback
64  def _async_update_attrs(self) -> None:
65  """Handle updating _attr values."""
66  self._attr_is_on_attr_is_on = self._device_device.status
67 
68  async def async_turn_off(self, **kwargs: Any) -> None:
69  """Instruct the device to turn off."""
70  await self._device_device.turn_off()
71  await self.coordinator.async_request_refresh()
None __init__(self, WizData wiz_data, str name)
Definition: entity.py:27
None turn_off(self, **Any kwargs)
Definition: entity.py:1705