1 """Homematic base entity."""
3 from __future__
import annotations
5 from abc
import abstractmethod
6 from datetime
import timedelta
9 from pyhomematic
import HMConnection
10 from pyhomematic.devicetypes.generic
import HMGeneric
28 _LOGGER = logging.getLogger(__name__)
35 """The HomeMatic device base object."""
37 _homematic: HMConnection
39 _attr_should_poll =
False
43 config: dict[str, str],
44 entity_description: EntityDescription |
None =
None,
46 """Initialize a generic HomeMatic device."""
47 self.
_name_name = config.get(ATTR_NAME)
48 self.
_address_address = config.get(ATTR_ADDRESS)
50 self.
_channel_channel = config.get(ATTR_CHANNEL)
51 self.
_state_state = config.get(ATTR_PARAM)
53 self._data: dict[str, str] = {}
56 self._channel_map: dict[str, str] = {}
58 if entity_description
is not None:
66 """Load data init callbacks."""
71 """Return unique ID. HomeMatic entity IDs are unique by default."""
72 return self.
_unique_id_unique_id.replace(
" ",
"_")
76 """Return the name of the device."""
77 return self.
_name_name
81 """Return true if device is available."""
86 """Return device specific state attributes."""
94 for node, data
in HM_ATTRIBUTE_SUPPORT.items():
96 if node
in self._data:
97 value = data[1].
get(self._data[node], self._data[node])
103 """Connect to HomeMatic init values."""
119 except Exception
as err:
121 _LOGGER.error(
"Exception while linking %s: %s", self.
_address_address,
str(err))
124 """Handle all pyhomematic device events."""
128 if device.partition(
":")[2] == self._channel_map.
get(attribute):
129 self._data[attribute] = value
142 """Subscribe all required events to handle job."""
151 for node, channels
in metadata.items():
153 if node
in self._data:
155 if len(channels) == 1:
156 channel = channels[0]
160 self._channel_map[node] =
str(channel)
162 _LOGGER.debug(
"Channel map for %s: %s", self.
_address_address,
str(self._channel_map))
168 """Load first value from pyhomematic."""
173 for metadata, funct
in (
179 for node
in metadata:
180 if metadata[node]
and node
in self._data:
181 self._data[node] = funct(name=node, channel=self.
_channel_channel)
186 """Set data to main datapoint."""
187 if self.
_state_state
in self._data:
188 self._data[self.
_state_state] = value
191 """Get data from main datapoint."""
192 if self.
_state_state
in self._data:
193 return self._data[self.
_state_state]
197 """Generate a data dict (self._data) from the HomeMatic metadata."""
199 for data_note
in self.
_hmdevice_hmdevice.ATTRIBUTENODE:
200 self._data.
update({data_note:
None})
207 """Generate a data dictionary from the HomeMatic device metadata."""
211 """The HomeMatic hub. (CCU2/HomeGear)."""
213 _attr_should_poll =
False
216 """Initialize HomeMatic hub."""
233 """Return the name of the device."""
234 return self.
_name_name
238 """Return the state of the entity."""
243 """Return the state attributes."""
248 """Return the icon to use in the frontend, if any."""
249 return "mdi:gradient-vertical"
252 """Retrieve latest state."""
253 service_message = self.
_homematic_homematic.getServiceMessages(self.
_name_name)
254 state =
None if service_message
is None else len(service_message)
257 if self.
_state_state != state:
262 """Retrieve all variable data and update hmvariable states."""
263 variables = self.
_homematic_homematic.getAllSystemVariables(self.
_name_name)
264 if variables
is None:
268 for key, value
in variables.items():
279 """Set variable value on CCU/Homegear."""
281 _LOGGER.error(
"Variable %s not found on %s", name, self.
namenamename)
284 if isinstance(old_value, bool):
285 value = cv.boolean(value)
def _subscribe_homematic_events(self)
def _load_data_from_hm(self)
None __init__(self, dict[str, str] config, EntityDescription|None entity_description=None)
def _hm_set_state(self, value)
def _hm_event_callback(self, device, caller, attribute, value)
def _init_data_struct(self)
def async_added_to_hass(self)
def extra_state_attributes(self)
def extra_state_attributes(self)
def _update_hub(self, now)
def _update_variables(self, now)
def hm_set_variable(self, name, value)
def __init__(self, hass, homematic, name)
None schedule_update_ha_state(self, bool force_refresh=False)
str|UndefinedType|None name(self)
web.Response get(self, web.Request request, str config_key)