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 <erik@montnemery.com>

* Add missing import

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
RJPoelstra 2024-05-10 09:58:16 +02:00 committed by GitHub
parent d4fbaef4f6
commit 8c54587d7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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()