core/homeassistant/components/motionmount/entity.py

54 lines
1.7 KiB
Python
Raw Normal View History

Add integration for Vogel's MotionMount (#103498) * Skeleton for Vogel's MotionMount support. * Generated updates. * Add validation of the discovered information. * Add manual configuration * Use a mac address as a unique id * Add tests for config_flow * Add a 'turn' sensor entity. * Add all needed sensors. * Add number and select entity for control of MotionMount * Update based on development checklist * Preset selector now updates when a preset is chosen * Fix adding presets selector to device * Remove irrelevant TODO * Bump python-MotionMount requirement * Invert direction of turn slider * Prepare for PR * Make sure entities have correct values when created * Use device's mac address as unique id for entities. * Fix missing files in .coveragerc * Remove typing ignore from device library. Improved typing also gave rise to the need to improve the callback mechanism * Improve typing * Convert property to shorthand form * Remove unneeded CONF_NAME in ConfigEntry * Add small comment * Refresh coordinator on notification from MotionMount * Use translation for entity * Bump python-MotionMount * Raise `ConfigEntryNotReady` when connect fails * Use local variable * Improve exception handling * Reduce duplicate code * Make better use of constants * Remove unneeded callback * Remove other occurrence of unneeded callback * Improve removal of suffix * Catch 'getaddrinfo' exception * Add config flow tests for invalid hostname * Abort if device with same hostname is already configured * Make sure we connect to a device with the same unique id as configured * Convert function names to snake_case * Remove unneeded commented-out code * Use tuple * Make us of config_entry id when mac is missing * Prevent update of entities when nothing changed * Don't store data in `hass.data` until we know we will proceed * Remove coordinator * Handle situation where mac is EMPTY_MAC * Disable polling * Fix failing hassfest * Avoid calling unique-id-less discovery handler for situations where we've an unique id
2023-12-22 11:04:58 +00:00
"""Support for MotionMount sensors."""
import motionmount
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_CONNECTIONS, ATTR_IDENTIFIERS
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo, format_mac
from homeassistant.helpers.entity import Entity
from .const import DOMAIN, EMPTY_MAC
class MotionMountEntity(Entity):
"""Representation of a MotionMount entity."""
_attr_should_poll = False
_attr_has_entity_name = True
def __init__(self, mm: motionmount.MotionMount, config_entry: ConfigEntry) -> None:
"""Initialize general MotionMount entity."""
self.mm = mm
mac = format_mac(mm.mac.hex())
# Create a base unique id
if mac == EMPTY_MAC:
self._base_unique_id = config_entry.entry_id
else:
self._base_unique_id = mac
# Set device info
self._attr_device_info = DeviceInfo(
name=mm.name,
manufacturer="Vogel's",
model="TVM 7675",
)
if mac == EMPTY_MAC:
self._attr_device_info[ATTR_IDENTIFIERS] = {(DOMAIN, config_entry.entry_id)}
else:
self._attr_device_info[ATTR_CONNECTIONS] = {
(dr.CONNECTION_NETWORK_MAC, mac)
}
async def async_added_to_hass(self) -> None:
"""Store register state change callback."""
self.mm.add_listener(self.async_write_ha_state)
await super().async_added_to_hass()
async def async_will_remove_from_hass(self) -> None:
"""Remove register state change callback."""
self.mm.remove_listener(self.async_write_ha_state)
await super().async_will_remove_from_hass()