mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
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:
parent
190393c6bb
commit
e0424c8322
@ -3,11 +3,15 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from ihcsdk.ihccontroller import IHCController
|
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.const import CONF_TYPE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
from homeassistant.util.enum import try_parse_enum
|
||||||
|
|
||||||
from .const import CONF_INVERTING, DOMAIN, IHC_CONTROLLER
|
from .const import CONF_INVERTING, DOMAIN, IHC_CONTROLLER
|
||||||
from .ihcdevice import IHCDevice
|
from .ihcdevice import IHCDevice
|
||||||
@ -62,24 +66,13 @@ class IHCBinarySensor(IHCDevice, BinarySensorEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the IHC binary sensor."""
|
"""Initialize the IHC binary sensor."""
|
||||||
super().__init__(ihc_controller, controller_id, name, ihc_id, product)
|
super().__init__(ihc_controller, controller_id, name, ihc_id, product)
|
||||||
self._state = None
|
self._attr_device_class = try_parse_enum(BinarySensorDeviceClass, sensor_type)
|
||||||
self._sensor_type = sensor_type
|
|
||||||
self.inverting = inverting
|
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):
|
def on_ihc_change(self, ihc_id, value):
|
||||||
"""IHC resource has changed."""
|
"""IHC resource has changed."""
|
||||||
if self.inverting:
|
if self.inverting:
|
||||||
self._state = not value
|
self._attr_is_on = not value
|
||||||
else:
|
else:
|
||||||
self._state = value
|
self._attr_is_on = value
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
@ -51,29 +51,11 @@ class IHCSensor(IHCDevice, SensorEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the IHC sensor."""
|
"""Initialize the IHC sensor."""
|
||||||
super().__init__(ihc_controller, controller_id, name, ihc_id, product)
|
super().__init__(ihc_controller, controller_id, name, ihc_id, product)
|
||||||
self._state = None
|
self._attr_native_unit_of_measurement = unit
|
||||||
self._unit_of_measurement = unit
|
if unit in TEMPERATURE_UNITS:
|
||||||
|
self._attr_device_class = SensorDeviceClass.TEMPERATURE
|
||||||
@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
|
|
||||||
|
|
||||||
def on_ihc_change(self, ihc_id, value):
|
def on_ihc_change(self, ihc_id, value):
|
||||||
"""Handle IHC resource change."""
|
"""Handle IHC resource change."""
|
||||||
self._state = value
|
self._attr_native_value = value
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
@ -59,12 +59,6 @@ class IHCSwitch(IHCDevice, SwitchEntity):
|
|||||||
super().__init__(ihc_controller, controller_id, name, ihc_id, product)
|
super().__init__(ihc_controller, controller_id, name, ihc_id, product)
|
||||||
self._ihc_off_id = ihc_off_id
|
self._ihc_off_id = ihc_off_id
|
||||||
self._ihc_on_id = ihc_on_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:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the switch on."""
|
"""Turn the switch on."""
|
||||||
@ -82,5 +76,5 @@ class IHCSwitch(IHCDevice, SwitchEntity):
|
|||||||
|
|
||||||
def on_ihc_change(self, ihc_id, value):
|
def on_ihc_change(self, ihc_id, value):
|
||||||
"""Handle IHC resource change."""
|
"""Handle IHC resource change."""
|
||||||
self._state = value
|
self._attr_is_on = value
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user