From 8c54587d7ec5c8c5a58f512f21a61358adcd7d56 Mon Sep 17 00:00:00 2001 From: RJPoelstra <36924801+RJPoelstra@users.noreply.github.com> Date: Fri, 10 May 2024 09:58:16 +0200 Subject: [PATCH] Improve base entity state in Vogel's MotionMount integration (#109043) * Update device info when name changes * Entities now report themselves as being unavailable when the MotionMount is disconnected * Don't update device_info when name changes * Use `device_entry` property to update device name * Assert device is available Co-authored-by: Erik Montnemery * Add missing import --------- Co-authored-by: Erik Montnemery --- homeassistant/components/motionmount/entity.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/homeassistant/components/motionmount/entity.py b/homeassistant/components/motionmount/entity.py index c3f7c9c9358..8403af05491 100644 --- a/homeassistant/components/motionmount/entity.py +++ b/homeassistant/components/motionmount/entity.py @@ -1,5 +1,7 @@ """Support for MotionMount sensors.""" +from typing import TYPE_CHECKING + import motionmount from homeassistant.config_entries import ConfigEntry @@ -42,12 +44,28 @@ class MotionMountEntity(Entity): (dr.CONNECTION_NETWORK_MAC, mac) } + @property + def available(self) -> bool: + """Return True if the MotionMount is available (we're connected).""" + return self.mm.is_connected + + def update_name(self) -> None: + """Update the name of the associated device.""" + if TYPE_CHECKING: + assert self.device_entry + # Update the name in the device registry if needed + if self.device_entry.name != self.mm.name: + device_registry = dr.async_get(self.hass) + device_registry.async_update_device(self.device_entry.id, name=self.mm.name) + async def async_added_to_hass(self) -> None: """Store register state change callback.""" self.mm.add_listener(self.async_write_ha_state) + self.mm.add_listener(self.update_name) 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) + self.mm.remove_listener(self.update_name) await super().async_will_remove_from_hass()