Refactor hive entity (#72311)
* Add hive category entity changes * Updates based on PR feedback * Revert libary bump * Update after PR feedback * Update Binary device class for smoke sensor Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Remove entity category Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Updates after PR review * Remove unused import * Update light based on PR feedback * Update light code from PR review Co-authored-by: Martin Hjelmare <marhje52@gmail.com>pull/72677/head
parent
e7e48cd9f6
commit
d59ecc4c96
|
@ -22,7 +22,7 @@ from homeassistant.helpers.dispatcher import (
|
|||
async_dispatcher_connect,
|
||||
async_dispatcher_send,
|
||||
)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import DOMAIN, PLATFORM_LOOKUP, PLATFORMS
|
||||
|
@ -132,8 +132,17 @@ class HiveEntity(Entity):
|
|||
"""Initialize the instance."""
|
||||
self.hive = hive
|
||||
self.device = hive_device
|
||||
self._attr_name = self.device["haName"]
|
||||
self._attr_unique_id = f'{self.device["hiveID"]}-{self.device["hiveType"]}'
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, self.device["device_id"])},
|
||||
model=self.device["deviceData"]["model"],
|
||||
manufacturer=self.device["deviceData"]["manufacturer"],
|
||||
name=self.device["device_name"],
|
||||
sw_version=self.device["deviceData"]["version"],
|
||||
via_device=(DOMAIN, self.device["parentDevice"]),
|
||||
)
|
||||
self.attributes = {}
|
||||
self._unique_id = f'{self.device["hiveID"]}-{self.device["hiveType"]}'
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""When entity is added to Home Assistant."""
|
||||
|
|
|
@ -13,7 +13,6 @@ from homeassistant.const import (
|
|||
STATE_ALARM_TRIGGERED,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import HiveEntity
|
||||
|
@ -50,40 +49,6 @@ class HiveAlarmControlPanelEntity(HiveEntity, AlarmControlPanelEntity):
|
|||
| AlarmControlPanelEntityFeature.ARM_AWAY
|
||||
)
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique ID of entity."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device information about this AdGuard Home instance."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, self.device["device_id"])},
|
||||
model=self.device["deviceData"]["model"],
|
||||
manufacturer=self.device["deviceData"]["manufacturer"],
|
||||
name=self.device["device_name"],
|
||||
sw_version=self.device["deviceData"]["version"],
|
||||
via_device=(DOMAIN, self.device["parentDevice"]),
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the alarm."""
|
||||
return self.device["haName"]
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return if the device is available."""
|
||||
return self.device["deviceData"]["online"]
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return state of alarm."""
|
||||
if self.device["status"]["state"]:
|
||||
return STATE_ALARM_TRIGGERED
|
||||
return HIVETOHA[self.device["status"]["mode"]]
|
||||
|
||||
async def async_alarm_disarm(self, code=None):
|
||||
"""Send disarm command."""
|
||||
await self.hive.alarm.setMode(self.device, "home")
|
||||
|
@ -100,3 +65,9 @@ class HiveAlarmControlPanelEntity(HiveEntity, AlarmControlPanelEntity):
|
|||
"""Update all Node data from Hive."""
|
||||
await self.hive.session.updateData(self.device)
|
||||
self.device = await self.hive.alarm.getAlarm(self.device)
|
||||
self._attr_available = self.device["deviceData"].get("online")
|
||||
if self._attr_available:
|
||||
if self.device["status"]["state"]:
|
||||
self._attr_state = STATE_ALARM_TRIGGERED
|
||||
else:
|
||||
self._attr_state = HIVETOHA[self.device["status"]["mode"]]
|
||||
|
|
|
@ -4,27 +4,46 @@ from datetime import timedelta
|
|||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import HiveEntity
|
||||
from .const import ATTR_MODE, DOMAIN
|
||||
from .const import DOMAIN
|
||||
|
||||
DEVICETYPE = {
|
||||
"contactsensor": BinarySensorDeviceClass.OPENING,
|
||||
"motionsensor": BinarySensorDeviceClass.MOTION,
|
||||
"Connectivity": BinarySensorDeviceClass.CONNECTIVITY,
|
||||
"SMOKE_CO": BinarySensorDeviceClass.SMOKE,
|
||||
"DOG_BARK": BinarySensorDeviceClass.SOUND,
|
||||
"GLASS_BREAK": BinarySensorDeviceClass.SOUND,
|
||||
}
|
||||
PARALLEL_UPDATES = 0
|
||||
SCAN_INTERVAL = timedelta(seconds=15)
|
||||
|
||||
|
||||
BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
||||
BinarySensorEntityDescription(
|
||||
key="contactsensor", device_class=BinarySensorDeviceClass.OPENING
|
||||
),
|
||||
BinarySensorEntityDescription(
|
||||
key="motionsensor",
|
||||
device_class=BinarySensorDeviceClass.MOTION,
|
||||
),
|
||||
BinarySensorEntityDescription(
|
||||
key="Connectivity",
|
||||
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||
),
|
||||
BinarySensorEntityDescription(
|
||||
key="SMOKE_CO",
|
||||
device_class=BinarySensorDeviceClass.SMOKE,
|
||||
),
|
||||
BinarySensorEntityDescription(
|
||||
key="DOG_BARK",
|
||||
device_class=BinarySensorDeviceClass.SOUND,
|
||||
),
|
||||
BinarySensorEntityDescription(
|
||||
key="GLASS_BREAK",
|
||||
device_class=BinarySensorDeviceClass.SOUND,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
|
@ -34,62 +53,28 @@ async def async_setup_entry(
|
|||
devices = hive.session.deviceList.get("binary_sensor")
|
||||
entities = []
|
||||
if devices:
|
||||
for dev in devices:
|
||||
entities.append(HiveBinarySensorEntity(hive, dev))
|
||||
for description in BINARY_SENSOR_TYPES:
|
||||
for dev in devices:
|
||||
if dev["hiveType"] == description.key:
|
||||
entities.append(HiveBinarySensorEntity(hive, dev, description))
|
||||
async_add_entities(entities, True)
|
||||
|
||||
|
||||
class HiveBinarySensorEntity(HiveEntity, BinarySensorEntity):
|
||||
"""Representation of a Hive binary sensor."""
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique ID of entity."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device information."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, self.device["device_id"])},
|
||||
manufacturer=self.device["deviceData"]["manufacturer"],
|
||||
model=self.device["deviceData"]["model"],
|
||||
name=self.device["device_name"],
|
||||
sw_version=self.device["deviceData"]["version"],
|
||||
via_device=(DOMAIN, self.device["parentDevice"]),
|
||||
)
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the class of this sensor."""
|
||||
return DEVICETYPE.get(self.device["hiveType"])
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the binary sensor."""
|
||||
return self.device["haName"]
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return if the device is available."""
|
||||
if self.device["hiveType"] != "Connectivity":
|
||||
return self.device["deviceData"]["online"]
|
||||
return True
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Show Device Attributes."""
|
||||
return {
|
||||
ATTR_MODE: self.attributes.get(ATTR_MODE),
|
||||
}
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the binary sensor is on."""
|
||||
return self.device["status"]["state"]
|
||||
def __init__(self, hive, hive_device, entity_description):
|
||||
"""Initialise hive binary sensor."""
|
||||
super().__init__(hive, hive_device)
|
||||
self.entity_description = entity_description
|
||||
|
||||
async def async_update(self):
|
||||
"""Update all Node data from Hive."""
|
||||
await self.hive.session.updateData(self.device)
|
||||
self.device = await self.hive.sensor.getSensor(self.device)
|
||||
self.attributes = self.device.get("attributes", {})
|
||||
self._attr_is_on = self.device["status"]["state"]
|
||||
if self.device["hiveType"] != "Connectivity":
|
||||
self._attr_available = self.device["deviceData"].get("online")
|
||||
else:
|
||||
self._attr_available = True
|
||||
|
|
|
@ -16,7 +16,6 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import HiveEntity, refresh_system
|
||||
|
@ -46,8 +45,6 @@ HIVE_TO_HASS_HVAC_ACTION = {
|
|||
}
|
||||
|
||||
TEMP_UNIT = {"C": TEMP_CELSIUS, "F": TEMP_FAHRENHEIT}
|
||||
|
||||
SUPPORT_PRESET = [PRESET_NONE, PRESET_BOOST]
|
||||
PARALLEL_UPDATES = 0
|
||||
SCAN_INTERVAL = timedelta(seconds=15)
|
||||
_LOGGER = logging.getLogger()
|
||||
|
@ -105,6 +102,7 @@ class HiveClimateEntity(HiveEntity, ClimateEntity):
|
|||
"""Hive Climate Device."""
|
||||
|
||||
_attr_hvac_modes = [HVACMode.AUTO, HVACMode.HEAT, HVACMode.OFF]
|
||||
_attr_preset_modes = [PRESET_BOOST, PRESET_NONE]
|
||||
_attr_supported_features = (
|
||||
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
|
||||
)
|
||||
|
@ -113,84 +111,7 @@ class HiveClimateEntity(HiveEntity, ClimateEntity):
|
|||
"""Initialize the Climate device."""
|
||||
super().__init__(hive_session, hive_device)
|
||||
self.thermostat_node_id = hive_device["device_id"]
|
||||
self.temperature_type = TEMP_UNIT.get(hive_device["temperatureunit"])
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique ID of entity."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device information."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, self.device["device_id"])},
|
||||
manufacturer=self.device["deviceData"]["manufacturer"],
|
||||
model=self.device["deviceData"]["model"],
|
||||
name=self.device["device_name"],
|
||||
sw_version=self.device["deviceData"]["version"],
|
||||
via_device=(DOMAIN, self.device["parentDevice"]),
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the Climate device."""
|
||||
return self.device["haName"]
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return if the device is available."""
|
||||
return self.device["deviceData"]["online"]
|
||||
|
||||
@property
|
||||
def hvac_mode(self) -> HVACMode:
|
||||
"""Return hvac operation ie. heat, cool mode.
|
||||
|
||||
Need to be one of HVAC_MODE_*.
|
||||
"""
|
||||
return HIVE_TO_HASS_STATE[self.device["status"]["mode"]]
|
||||
|
||||
@property
|
||||
def hvac_action(self) -> HVACAction:
|
||||
"""Return current HVAC action."""
|
||||
return HIVE_TO_HASS_HVAC_ACTION[self.device["status"]["action"]]
|
||||
|
||||
@property
|
||||
def temperature_unit(self):
|
||||
"""Return the unit of measurement."""
|
||||
return self.temperature_type
|
||||
|
||||
@property
|
||||
def current_temperature(self):
|
||||
"""Return the current temperature."""
|
||||
return self.device["status"]["current_temperature"]
|
||||
|
||||
@property
|
||||
def target_temperature(self):
|
||||
"""Return the target temperature."""
|
||||
return self.device["status"]["target_temperature"]
|
||||
|
||||
@property
|
||||
def min_temp(self):
|
||||
"""Return minimum temperature."""
|
||||
return self.device["min_temp"]
|
||||
|
||||
@property
|
||||
def max_temp(self):
|
||||
"""Return the maximum temperature."""
|
||||
return self.device["max_temp"]
|
||||
|
||||
@property
|
||||
def preset_mode(self):
|
||||
"""Return the current preset mode, e.g., home, away, temp."""
|
||||
if self.device["status"]["boost"] == "ON":
|
||||
return PRESET_BOOST
|
||||
return PRESET_NONE
|
||||
|
||||
@property
|
||||
def preset_modes(self):
|
||||
"""Return a list of available preset modes."""
|
||||
return SUPPORT_PRESET
|
||||
self._attr_temperature_unit = TEMP_UNIT.get(hive_device["temperatureunit"])
|
||||
|
||||
@refresh_system
|
||||
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
|
||||
|
@ -236,3 +157,19 @@ class HiveClimateEntity(HiveEntity, ClimateEntity):
|
|||
"""Update all Node data from Hive."""
|
||||
await self.hive.session.updateData(self.device)
|
||||
self.device = await self.hive.heating.getClimate(self.device)
|
||||
self._attr_available = self.device["deviceData"].get("online")
|
||||
if self._attr_available:
|
||||
self._attr_hvac_mode = HIVE_TO_HASS_STATE[self.device["status"]["mode"]]
|
||||
self._attr_hvac_action = HIVE_TO_HASS_HVAC_ACTION[
|
||||
self.device["status"]["action"]
|
||||
]
|
||||
self._attr_current_temperature = self.device["status"][
|
||||
"current_temperature"
|
||||
]
|
||||
self._attr_target_temperature = self.device["status"]["target_temperature"]
|
||||
self._attr_min_temp = self.device["min_temp"]
|
||||
self._attr_max_temp = self.device["max_temp"]
|
||||
if self.device["status"]["boost"] == "ON":
|
||||
self._attr_preset_mode = PRESET_BOOST
|
||||
else:
|
||||
self._attr_preset_mode = PRESET_NONE
|
||||
|
|
|
@ -12,7 +12,6 @@ from homeassistant.components.light import (
|
|||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
import homeassistant.util.color as color_util
|
||||
|
||||
|
@ -40,72 +39,18 @@ async def async_setup_entry(
|
|||
class HiveDeviceLight(HiveEntity, LightEntity):
|
||||
"""Hive Active Light Device."""
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique ID of entity."""
|
||||
return self._unique_id
|
||||
def __init__(self, hive, hive_device):
|
||||
"""Initialise hive light."""
|
||||
super().__init__(hive, hive_device)
|
||||
if self.device["hiveType"] == "warmwhitelight":
|
||||
self._attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
||||
elif self.device["hiveType"] == "tuneablelight":
|
||||
self._attr_supported_color_modes = {ColorMode.COLOR_TEMP}
|
||||
elif self.device["hiveType"] == "colourtuneablelight":
|
||||
self._attr_supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS}
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device information."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, self.device["device_id"])},
|
||||
manufacturer=self.device["deviceData"]["manufacturer"],
|
||||
model=self.device["deviceData"]["model"],
|
||||
name=self.device["device_name"],
|
||||
sw_version=self.device["deviceData"]["version"],
|
||||
via_device=(DOMAIN, self.device["parentDevice"]),
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the display name of this light."""
|
||||
return self.device["haName"]
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return if the device is available."""
|
||||
return self.device["deviceData"]["online"]
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Show Device Attributes."""
|
||||
return {
|
||||
ATTR_MODE: self.attributes.get(ATTR_MODE),
|
||||
}
|
||||
|
||||
@property
|
||||
def brightness(self):
|
||||
"""Brightness of the light (an integer in the range 1-255)."""
|
||||
return self.device["status"]["brightness"]
|
||||
|
||||
@property
|
||||
def min_mireds(self):
|
||||
"""Return the coldest color_temp that this light supports."""
|
||||
return self.device.get("min_mireds")
|
||||
|
||||
@property
|
||||
def max_mireds(self):
|
||||
"""Return the warmest color_temp that this light supports."""
|
||||
return self.device.get("max_mireds")
|
||||
|
||||
@property
|
||||
def color_temp(self):
|
||||
"""Return the CT color value in mireds."""
|
||||
return self.device["status"].get("color_temp")
|
||||
|
||||
@property
|
||||
def hs_color(self):
|
||||
"""Return the hs color value."""
|
||||
if self.device["status"]["mode"] == "COLOUR":
|
||||
rgb = self.device["status"].get("hs_color")
|
||||
return color_util.color_RGB_to_hs(*rgb)
|
||||
return None
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if light is on."""
|
||||
return self.device["status"]["state"]
|
||||
self._attr_min_mireds = self.device.get("min_mireds")
|
||||
self._attr_max_mireds = self.device.get("max_mireds")
|
||||
|
||||
@refresh_system
|
||||
async def async_turn_on(self, **kwargs):
|
||||
|
@ -137,32 +82,18 @@ class HiveDeviceLight(HiveEntity, LightEntity):
|
|||
"""Instruct the light to turn off."""
|
||||
await self.hive.light.turnOff(self.device)
|
||||
|
||||
@property
|
||||
def color_mode(self) -> str:
|
||||
"""Return the color mode of the light."""
|
||||
if self.device["hiveType"] == "warmwhitelight":
|
||||
return ColorMode.BRIGHTNESS
|
||||
if self.device["hiveType"] == "tuneablelight":
|
||||
return ColorMode.COLOR_TEMP
|
||||
if self.device["hiveType"] == "colourtuneablelight":
|
||||
if self.device["status"]["mode"] == "COLOUR":
|
||||
return ColorMode.HS
|
||||
return ColorMode.COLOR_TEMP
|
||||
return ColorMode.ONOFF
|
||||
|
||||
@property
|
||||
def supported_color_modes(self) -> set[str] | None:
|
||||
"""Flag supported color modes."""
|
||||
if self.device["hiveType"] == "warmwhitelight":
|
||||
return {ColorMode.BRIGHTNESS}
|
||||
if self.device["hiveType"] == "tuneablelight":
|
||||
return {ColorMode.COLOR_TEMP}
|
||||
if self.device["hiveType"] == "colourtuneablelight":
|
||||
return {ColorMode.COLOR_TEMP, ColorMode.HS}
|
||||
return {ColorMode.ONOFF}
|
||||
|
||||
async def async_update(self):
|
||||
"""Update all Node data from Hive."""
|
||||
await self.hive.session.updateData(self.device)
|
||||
self.device = await self.hive.light.getLight(self.device)
|
||||
self.attributes.update(self.device.get("attributes", {}))
|
||||
self._attr_extra_state_attributes = {
|
||||
ATTR_MODE: self.attributes.get(ATTR_MODE),
|
||||
}
|
||||
self._attr_available = self.device["deviceData"].get("online")
|
||||
if self._attr_available:
|
||||
self._attr_is_on = self.device["status"]["state"]
|
||||
self._attr_brightness = self.device["status"]["brightness"]
|
||||
if self.device["hiveType"] == "colourtuneablelight":
|
||||
rgb = self.device["status"]["hs_color"]
|
||||
self._attr_hs_color = color_util.color_RGB_to_hs(*rgb)
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
"""Support for the Hive sensors."""
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import PERCENTAGE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import HiveEntity
|
||||
|
@ -12,71 +16,41 @@ from .const import DOMAIN
|
|||
|
||||
PARALLEL_UPDATES = 0
|
||||
SCAN_INTERVAL = timedelta(seconds=15)
|
||||
DEVICETYPE = {
|
||||
"Battery": {"unit": " % ", "type": SensorDeviceClass.BATTERY},
|
||||
}
|
||||
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
key="Battery",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
device_class=SensorDeviceClass.BATTERY,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up Hive thermostat based on a config entry."""
|
||||
|
||||
hive = hass.data[DOMAIN][entry.entry_id]
|
||||
devices = hive.session.deviceList.get("sensor")
|
||||
entities = []
|
||||
if devices:
|
||||
for dev in devices:
|
||||
entities.append(HiveSensorEntity(hive, dev))
|
||||
for description in SENSOR_TYPES:
|
||||
for dev in devices:
|
||||
if dev["hiveType"] == description.key:
|
||||
entities.append(HiveSensorEntity(hive, dev, description))
|
||||
async_add_entities(entities, True)
|
||||
|
||||
|
||||
class HiveSensorEntity(HiveEntity, SensorEntity):
|
||||
"""Hive Sensor Entity."""
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique ID of entity."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device information."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, self.device["device_id"])},
|
||||
manufacturer=self.device["deviceData"]["manufacturer"],
|
||||
model=self.device["deviceData"]["model"],
|
||||
name=self.device["device_name"],
|
||||
sw_version=self.device["deviceData"]["version"],
|
||||
via_device=(DOMAIN, self.device["parentDevice"]),
|
||||
)
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return if sensor is available."""
|
||||
return self.device.get("deviceData", {}).get("online")
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Device class of the entity."""
|
||||
return DEVICETYPE[self.device["hiveType"]].get("type")
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self):
|
||||
"""Return the unit of measurement."""
|
||||
return DEVICETYPE[self.device["hiveType"]].get("unit")
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self.device["haName"]
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self.device["status"]["state"]
|
||||
def __init__(self, hive, hive_device, entity_description):
|
||||
"""Initialise hive sensor."""
|
||||
super().__init__(hive, hive_device)
|
||||
self.entity_description = entity_description
|
||||
|
||||
async def async_update(self):
|
||||
"""Update all Node data from Hive."""
|
||||
await self.hive.session.updateData(self.device)
|
||||
self.device = await self.hive.sensor.getSensor(self.device)
|
||||
self._attr_native_value = self.device["status"]["state"]
|
||||
|
|
|
@ -3,10 +3,9 @@ from __future__ import annotations
|
|||
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import HiveEntity, refresh_system
|
||||
|
@ -16,6 +15,14 @@ PARALLEL_UPDATES = 0
|
|||
SCAN_INTERVAL = timedelta(seconds=15)
|
||||
|
||||
|
||||
SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
|
||||
SwitchEntityDescription(
|
||||
key="activeplug",
|
||||
),
|
||||
SwitchEntityDescription(key="Heating_Heat_On_Demand"),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
|
@ -25,54 +32,20 @@ async def async_setup_entry(
|
|||
devices = hive.session.deviceList.get("switch")
|
||||
entities = []
|
||||
if devices:
|
||||
for dev in devices:
|
||||
entities.append(HiveDevicePlug(hive, dev))
|
||||
for description in SWITCH_TYPES:
|
||||
for dev in devices:
|
||||
if dev["hiveType"] == description.key:
|
||||
entities.append(HiveSwitch(hive, dev, description))
|
||||
async_add_entities(entities, True)
|
||||
|
||||
|
||||
class HiveDevicePlug(HiveEntity, SwitchEntity):
|
||||
class HiveSwitch(HiveEntity, SwitchEntity):
|
||||
"""Hive Active Plug."""
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique ID of entity."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo | None:
|
||||
"""Return device information."""
|
||||
if self.device["hiveType"] == "activeplug":
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, self.device["device_id"])},
|
||||
manufacturer=self.device["deviceData"]["manufacturer"],
|
||||
model=self.device["deviceData"]["model"],
|
||||
name=self.device["device_name"],
|
||||
sw_version=self.device["deviceData"]["version"],
|
||||
via_device=(DOMAIN, self.device["parentDevice"]),
|
||||
)
|
||||
return None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of this Switch device if any."""
|
||||
return self.device["haName"]
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return if the device is available."""
|
||||
return self.device["deviceData"].get("online")
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Show Device Attributes."""
|
||||
return {
|
||||
ATTR_MODE: self.attributes.get(ATTR_MODE),
|
||||
}
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if switch is on."""
|
||||
return self.device["status"]["state"]
|
||||
def __init__(self, hive, hive_device, entity_description):
|
||||
"""Initialise hive switch."""
|
||||
super().__init__(hive, hive_device)
|
||||
self.entity_description = entity_description
|
||||
|
||||
@refresh_system
|
||||
async def async_turn_on(self, **kwargs):
|
||||
|
@ -89,3 +62,9 @@ class HiveDevicePlug(HiveEntity, SwitchEntity):
|
|||
await self.hive.session.updateData(self.device)
|
||||
self.device = await self.hive.switch.getSwitch(self.device)
|
||||
self.attributes.update(self.device.get("attributes", {}))
|
||||
self._attr_extra_state_attributes = {
|
||||
ATTR_MODE: self.attributes.get(ATTR_MODE),
|
||||
}
|
||||
self._attr_available = self.device["deviceData"].get("online")
|
||||
if self._attr_available:
|
||||
self._attr_is_on = self.device["status"]["state"]
|
||||
|
|
|
@ -12,7 +12,6 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.const import STATE_OFF, STATE_ON, TEMP_CELSIUS
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import HiveEntity, refresh_system
|
||||
|
@ -75,48 +74,8 @@ class HiveWaterHeater(HiveEntity, WaterHeaterEntity):
|
|||
"""Hive Water Heater Device."""
|
||||
|
||||
_attr_supported_features = WaterHeaterEntityFeature.OPERATION_MODE
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique ID of entity."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device information."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, self.device["device_id"])},
|
||||
manufacturer=self.device["deviceData"]["manufacturer"],
|
||||
model=self.device["deviceData"]["model"],
|
||||
name=self.device["device_name"],
|
||||
sw_version=self.device["deviceData"]["version"],
|
||||
via_device=(DOMAIN, self.device["parentDevice"]),
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the water heater."""
|
||||
return HOTWATER_NAME
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return if the device is available."""
|
||||
return self.device["deviceData"]["online"]
|
||||
|
||||
@property
|
||||
def temperature_unit(self):
|
||||
"""Return the unit of measurement."""
|
||||
return TEMP_CELSIUS
|
||||
|
||||
@property
|
||||
def current_operation(self):
|
||||
"""Return current operation."""
|
||||
return HIVE_TO_HASS_STATE[self.device["status"]["current_operation"]]
|
||||
|
||||
@property
|
||||
def operation_list(self):
|
||||
"""List of available operation modes."""
|
||||
return SUPPORT_WATER_HEATER
|
||||
_attr_temperature_unit = TEMP_CELSIUS
|
||||
_attr_operation_list = SUPPORT_WATER_HEATER
|
||||
|
||||
@refresh_system
|
||||
async def async_turn_on(self, **kwargs):
|
||||
|
@ -146,3 +105,8 @@ class HiveWaterHeater(HiveEntity, WaterHeaterEntity):
|
|||
"""Update all Node data from Hive."""
|
||||
await self.hive.session.updateData(self.device)
|
||||
self.device = await self.hive.hotwater.getWaterHeater(self.device)
|
||||
self._attr_available = self.device["deviceData"].get("online")
|
||||
if self._attr_available:
|
||||
self._attr_current_operation = HIVE_TO_HASS_STATE[
|
||||
self.device["status"]["current_operation"]
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue