Use shorthand attributes in Point (#100214)

This commit is contained in:
Joost Lekkerkerker 2023-09-12 17:15:36 +02:00 committed by GitHub
parent 54c034185f
commit 75951dd67b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 61 deletions

View file

@ -264,9 +264,20 @@ class MinutPointEntity(Entity):
self._client = point_client
self._id = device_id
self._name = self.device.name
self._device_class = device_class
self._attr_device_class = device_class
self._updated = utc_from_timestamp(0)
self._value = None
self._attr_unique_id = f"point.{device_id}-{device_class}"
device = self.device.device
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, device["device_mac"])},
identifiers={(DOMAIN, device["device_id"])},
manufacturer="Minut",
model=f"Point v{device['hardware_version']}",
name=device["description"],
sw_version=device["firmware"]["installed"],
via_device=(DOMAIN, device["home"]),
)
self._attr_name = f"{self._name} {device_class.capitalize()}"
def __str__(self):
"""Return string representation of device."""
@ -298,11 +309,6 @@ class MinutPointEntity(Entity):
"""Return the representation of the device."""
return self._client.device(self.device_id)
@property
def device_class(self):
"""Return the device class."""
return self._device_class
@property
def device_id(self):
"""Return the id of the device."""
@ -317,25 +323,6 @@ class MinutPointEntity(Entity):
)
return attrs
@property
def device_info(self) -> DeviceInfo:
"""Return a device description for device registry."""
device = self.device.device
return DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, device["device_mac"])},
identifiers={(DOMAIN, device["device_id"])},
manufacturer="Minut",
model=f"Point v{device['hardware_version']}",
name=device["description"],
sw_version=device["firmware"]["installed"],
via_device=(DOMAIN, device["home"]),
)
@property
def name(self):
"""Return the display name of this device."""
return f"{self._name} {self.device_class.capitalize()}"
@property
def is_updated(self):
"""Return true if sensor have been updated."""
@ -344,15 +331,4 @@ class MinutPointEntity(Entity):
@property
def last_update(self):
"""Return the last_update time for the device."""
last_update = parse_datetime(self.device.last_update)
return last_update
@property
def unique_id(self):
"""Return the unique id of the sensor."""
return f"point.{self._id}-{self.device_class}"
@property
def value(self):
"""Return the sensor value."""
return self._value
return parse_datetime(self.device.last_update)

View file

@ -76,6 +76,9 @@ class MinutPointBinarySensor(MinutPointEntity, BinarySensorEntity):
self._device_name = device_name
self._async_unsub_hook_dispatcher_connect = None
self._events = EVENTS[device_name]
self._attr_unique_id = f"point.{device_id}-{device_name}"
self._attr_icon = DEVICES[self._device_name].get("icon")
self._attr_name = f"{self._name} {device_name.capitalize()}"
async def async_added_to_hass(self) -> None:
"""Call when entity is added to HOme Assistant."""
@ -124,18 +127,3 @@ class MinutPointBinarySensor(MinutPointEntity, BinarySensorEntity):
else:
self._attr_is_on = _is_on
self.async_write_ha_state()
@property
def name(self):
"""Return the display name of this device."""
return f"{self._name} {self._device_name.capitalize()}"
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
return DEVICES[self._device_name].get("icon")
@property
def unique_id(self):
"""Return the unique id of the sensor."""
return f"point.{self._id}-{self._device_name}"

View file

@ -98,13 +98,10 @@ class MinutPointSensor(MinutPointEntity, SensorEntity):
"""Update the value of the sensor."""
_LOGGER.debug("Update sensor value for %s", self)
if self.is_updated:
self._value = await self.device.sensor(self.device_class)
self._attr_native_value = await self.device.sensor(self.device_class)
if self.native_value is not None:
self._attr_native_value = round(
self.native_value, self.entity_description.precision
)
self._updated = parse_datetime(self.device.last_update)
self.async_write_ha_state()
@property
def native_value(self):
"""Return the state of the sensor."""
if self.value is None:
return None
return round(self.value, self.entity_description.precision)