Use shorthand attributes in IHC (#90350)

* typings to make linter happy

* Moving device_class and native_value to init

* remove is_on and use attr_is_on

* Use try_parse_enum for sensor type

* Remove not needed sensor_type

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/ihc/sensor.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
Jens Østergaard Nielsen 2023-03-28 10:23:00 +02:00 committed by GitHub
parent 190393c6bb
commit e0424c8322
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 44 deletions

View file

@ -3,11 +3,15 @@ from __future__ import annotations
from ihcsdk.ihccontroller import IHCController
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.const import CONF_TYPE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.enum import try_parse_enum
from .const import CONF_INVERTING, DOMAIN, IHC_CONTROLLER
from .ihcdevice import IHCDevice
@ -62,24 +66,13 @@ class IHCBinarySensor(IHCDevice, BinarySensorEntity):
) -> None:
"""Initialize the IHC binary sensor."""
super().__init__(ihc_controller, controller_id, name, ihc_id, product)
self._state = None
self._sensor_type = sensor_type
self._attr_device_class = try_parse_enum(BinarySensorDeviceClass, sensor_type)
self.inverting = inverting
@property
def device_class(self):
"""Return the class of this sensor."""
return self._sensor_type
@property
def is_on(self):
"""Return true if the binary sensor is on/open."""
return self._state
def on_ihc_change(self, ihc_id, value):
"""IHC resource has changed."""
if self.inverting:
self._state = not value
self._attr_is_on = not value
else:
self._state = value
self._attr_is_on = value
self.schedule_update_ha_state()

View file

@ -51,29 +51,11 @@ class IHCSensor(IHCDevice, SensorEntity):
) -> None:
"""Initialize the IHC sensor."""
super().__init__(ihc_controller, controller_id, name, ihc_id, product)
self._state = None
self._unit_of_measurement = unit
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return (
SensorDeviceClass.TEMPERATURE
if self._unit_of_measurement in TEMPERATURE_UNITS
else None
)
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
self._attr_native_unit_of_measurement = unit
if unit in TEMPERATURE_UNITS:
self._attr_device_class = SensorDeviceClass.TEMPERATURE
def on_ihc_change(self, ihc_id, value):
"""Handle IHC resource change."""
self._state = value
self._attr_native_value = value
self.schedule_update_ha_state()

View file

@ -59,12 +59,6 @@ class IHCSwitch(IHCDevice, SwitchEntity):
super().__init__(ihc_controller, controller_id, name, ihc_id, product)
self._ihc_off_id = ihc_off_id
self._ihc_on_id = ihc_on_id
self._state = False
@property
def is_on(self):
"""Return true if switch is on."""
return self._state
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
@ -82,5 +76,5 @@ class IHCSwitch(IHCDevice, SwitchEntity):
def on_ihc_change(self, ihc_id, value):
"""Handle IHC resource change."""
self._state = value
self._attr_is_on = value
self.schedule_update_ha_state()