Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for monitoring a Smappee appliance binary sensor."""
2 
3 from __future__ import annotations
4 
6  BinarySensorDeviceClass,
7  BinarySensorEntity,
8 )
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.device_registry import DeviceInfo
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from . import SmappeeConfigEntry
14 from .const import DOMAIN
15 
16 BINARY_SENSOR_PREFIX = "Appliance"
17 PRESENCE_PREFIX = "Presence"
18 
19 ICON_MAPPING = {
20  "Car Charger": "mdi:car",
21  "Coffeemaker": "mdi:coffee",
22  "Clothes Dryer": "mdi:tumble-dryer",
23  "Clothes Iron": "mdi:hanger",
24  "Dishwasher": "mdi:dishwasher",
25  "Lights": "mdi:lightbulb",
26  "Fan": "mdi:fan",
27  "Freezer": "mdi:fridge",
28  "Microwave": "mdi:microwave",
29  "Oven": "mdi:stove",
30  "Refrigerator": "mdi:fridge",
31  "Stove": "mdi:stove",
32  "Washing Machine": "mdi:washing-machine",
33  "Water Pump": "mdi:water-pump",
34 }
35 
36 
38  hass: HomeAssistant,
39  config_entry: SmappeeConfigEntry,
40  async_add_entities: AddEntitiesCallback,
41 ) -> None:
42  """Set up the Smappee binary sensor."""
43  smappee_base = config_entry.runtime_data
44 
45  entities: list[BinarySensorEntity] = []
46  for service_location in smappee_base.smappee.service_locations.values():
47  for appliance_id, appliance in service_location.appliances.items():
48  if appliance.type != "Find me" and appliance.source_type == "NILM":
49  entities.append(
51  smappee_base=smappee_base,
52  service_location=service_location,
53  appliance_id=appliance_id,
54  appliance_name=appliance.name,
55  appliance_type=appliance.type,
56  )
57  )
58 
59  if not smappee_base.smappee.local_polling:
60  # presence value only available in cloud env
61  entities.append(SmappeePresence(smappee_base, service_location))
62 
63  async_add_entities(entities, True)
64 
65 
67  """Implementation of a Smappee presence binary sensor."""
68 
69  _attr_device_class = BinarySensorDeviceClass.PRESENCE
70 
71  def __init__(self, smappee_base, service_location):
72  """Initialize the Smappee sensor."""
73  self._smappee_base_smappee_base = smappee_base
74  self._service_location_service_location = service_location
75  self._attr_name_attr_name = (
76  f"{service_location.service_location_name} - {PRESENCE_PREFIX}"
77  )
78  self._attr_unique_id_attr_unique_id = (
79  f"{service_location.device_serial_number}-"
80  f"{service_location.service_location_id}-"
81  f"{BinarySensorDeviceClass.PRESENCE}"
82  )
83  self._attr_device_info_attr_device_info = DeviceInfo(
84  identifiers={(DOMAIN, service_location.device_serial_number)},
85  manufacturer="Smappee",
86  model=service_location.device_model,
87  name=service_location.service_location_name,
88  sw_version=service_location.firmware_version,
89  )
90 
91  async def async_update(self) -> None:
92  """Get the latest data from Smappee and update the state."""
93  await self._smappee_base_smappee_base.async_update()
94 
95  self._attr_is_on_attr_is_on = self._service_location_service_location.is_present
96 
97 
99  """Implementation of a Smappee binary sensor."""
100 
101  def __init__(
102  self,
103  smappee_base,
104  service_location,
105  appliance_id,
106  appliance_name,
107  appliance_type,
108  ):
109  """Initialize the Smappee sensor."""
110  self._smappee_base_smappee_base = smappee_base
111  self._service_location_service_location = service_location
112  self._appliance_id_appliance_id = appliance_id
113  self._attr_name_attr_name = (
114  f"{service_location.service_location_name} - "
115  f"{BINARY_SENSOR_PREFIX} - "
116  f"{appliance_name if appliance_name != '' else appliance_type}"
117  )
118  self._attr_unique_id_attr_unique_id = (
119  f"{service_location.device_serial_number}-"
120  f"{service_location.service_location_id}-"
121  f"appliance-{appliance_id}"
122  )
123  self._attr_device_info_attr_device_info = DeviceInfo(
124  identifiers={(DOMAIN, service_location.device_serial_number)},
125  manufacturer="Smappee",
126  model=service_location.device_model,
127  name=service_location.service_location_name,
128  sw_version=service_location.firmware_version,
129  )
130  self._attr_icon_attr_icon = ICON_MAPPING.get(appliance_type)
131 
132  async def async_update(self) -> None:
133  """Get the latest data from Smappee and update the state."""
134  await self._smappee_base_smappee_base.async_update()
135 
136  appliance = self._service_location_service_location.appliances.get(self._appliance_id_appliance_id)
137  self._attr_is_on_attr_is_on = bool(appliance.state)
def __init__(self, smappee_base, service_location, appliance_id, appliance_name, appliance_type)
def __init__(self, smappee_base, service_location)
None async_setup_entry(HomeAssistant hass, SmappeeConfigEntry config_entry, AddEntitiesCallback async_add_entities)