mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
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:
parent
d4fbaef4f6
commit
8c54587d7e
@ -1,5 +1,7 @@
|
|||||||
"""Support for MotionMount sensors."""
|
"""Support for MotionMount sensors."""
|
||||||
|
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import motionmount
|
import motionmount
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -42,12 +44,28 @@ class MotionMountEntity(Entity):
|
|||||||
(dr.CONNECTION_NETWORK_MAC, mac)
|
(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:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Store register state change callback."""
|
"""Store register state change callback."""
|
||||||
self.mm.add_listener(self.async_write_ha_state)
|
self.mm.add_listener(self.async_write_ha_state)
|
||||||
|
self.mm.add_listener(self.update_name)
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self) -> None:
|
async def async_will_remove_from_hass(self) -> None:
|
||||||
"""Remove register state change callback."""
|
"""Remove register state change callback."""
|
||||||
self.mm.remove_listener(self.async_write_ha_state)
|
self.mm.remove_listener(self.async_write_ha_state)
|
||||||
|
self.mm.remove_listener(self.update_name)
|
||||||
await super().async_will_remove_from_hass()
|
await super().async_will_remove_from_hass()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user