From 38277d6ea3690e347f658048e00e6fc99eb4ddf6 Mon Sep 17 00:00:00 2001 From: Kirmas Date: Wed, 17 Aug 2022 19:53:02 +0000 Subject: [PATCH] cast entity_category string to EntityCategory enum - fix some precommit errors --- .../xiaomi_miio/ng_binary_sensor.py | 12 +++- .../components/xiaomi_miio/ng_select.py | 14 +++-- .../components/xiaomi_miio/ng_sensor.py | 59 +++++++++++-------- .../components/xiaomi_miio/ng_switch.py | 10 +++- 4 files changed, 59 insertions(+), 36 deletions(-) diff --git a/homeassistant/components/xiaomi_miio/ng_binary_sensor.py b/homeassistant/components/xiaomi_miio/ng_binary_sensor.py index e3491859dad06b..41b5c1dda8990d 100644 --- a/homeassistant/components/xiaomi_miio/ng_binary_sensor.py +++ b/homeassistant/components/xiaomi_miio/ng_binary_sensor.py @@ -1,16 +1,18 @@ """Support for Xiaomi Miio binary sensors.""" from __future__ import annotations +from collections.abc import Callable from dataclasses import dataclass import logging -from typing import Callable from homeassistant.components.binary_sensor import ( BinarySensorEntity, BinarySensorEntityDescription, ) -from homeassistant.components.xiaomi_miio.device import XiaomiCoordinatedMiioEntity from homeassistant.core import callback +from homeassistant.helpers.entity import EntityCategory + +from .device import XiaomiCoordinatedMiioEntity _LOGGER = logging.getLogger(__name__) @@ -37,12 +39,16 @@ def __init__(self, device, sensor, entry, coordinator): super().__init__(device, entry, unique_id, coordinator) + entity_category = None + if "entity_category" in sensor.extras: + entity_category = EntityCategory(sensor.extras.get("entity_category")) + description = XiaomiBinarySensorDescription( key=sensor.id, name=sensor.name, icon=sensor.extras.get("icon"), device_class=sensor.extras.get("device_class"), - entity_category=sensor.extras.get("entity_category"), + entity_category=entity_category, ) self.entity_description = description diff --git a/homeassistant/components/xiaomi_miio/ng_select.py b/homeassistant/components/xiaomi_miio/ng_select.py index f2cee4439512a0..0ae785a3be6b4f 100644 --- a/homeassistant/components/xiaomi_miio/ng_select.py +++ b/homeassistant/components/xiaomi_miio/ng_select.py @@ -4,8 +4,10 @@ import logging from homeassistant.components.select import SelectEntity, SelectEntityDescription -from homeassistant.components.xiaomi_miio.device import XiaomiCoordinatedMiioEntity from homeassistant.core import callback +from homeassistant.helpers.entity import EntityCategory + +from .device import XiaomiCoordinatedMiioEntity _LOGGER = logging.getLogger(__name__) @@ -23,16 +25,18 @@ def __init__(self, device, setting, entry, coordinator): super().__init__(device, entry, unique_id, coordinator) self._choices = setting.choices - self._attr_current_option = ( - None # TODO we don't know the value, but the parent wants it? - ) + self._attr_current_option = None # TODO we don't know the value, but the parent wants it? # pylint: disable=fixme + + entity_category = None + if "entity_category" in setting.extras: + entity_category = EntityCategory(setting.extras.get("entity_category")) self.entity_description = SelectEntityDescription( key=setting.id, name=setting.name, icon=setting.extras.get("icon"), device_class=setting.extras.get("device_class"), - entity_category=setting.extras.get("entity_category"), + entity_category=entity_category, # entity_category=EntityCategory.CONFIG, ) self._attr_options = [x.name for x in self._choices] diff --git a/homeassistant/components/xiaomi_miio/ng_sensor.py b/homeassistant/components/xiaomi_miio/ng_sensor.py index 2c9b89ddb1a093..3781c99b487bd6 100644 --- a/homeassistant/components/xiaomi_miio/ng_sensor.py +++ b/homeassistant/components/xiaomi_miio/ng_sensor.py @@ -3,15 +3,18 @@ import logging -from homeassistant.components.sensor import ( - SensorDeviceClass, +from homeassistant.components.sensor import ( # SensorDeviceClass, SensorEntity, SensorEntityDescription, ) -from homeassistant.components.xiaomi_miio.device import XiaomiCoordinatedMiioEntity -from homeassistant.components.xiaomi_miio.sensor import XiaomiMiioSensorDescription from homeassistant.core import callback -from homeassistant.util import dt as dt_util +from homeassistant.helpers.entity import EntityCategory + +from .device import XiaomiCoordinatedMiioEntity +from .sensor import XiaomiMiioSensorDescription + +# from homeassistant.util import dt as dt_util + _LOGGER = logging.getLogger(__name__) @@ -35,6 +38,10 @@ def __init__( unique_id = f"{entry.unique_id}_sensor_{sensor.id}" + entity_category = None + if "entity_category" in sensor.extras: + entity_category = EntityCategory(sensor.extras.get("entity_category")) + description = XiaomiMiioSensorDescription( key=sensor.id, name=sensor.name, @@ -42,7 +49,7 @@ def __init__( icon=sensor.extras.get("icon"), device_class=sensor.extras.get("device_class"), state_class=sensor.extras.get("state_class"), - entity_category=sensor.extras.get("entity_category"), + entity_category=entity_category, ) _LOGGER.debug("Adding sensor: %s", description) super().__init__(device, entry, unique_id, coordinator) @@ -64,23 +71,23 @@ def _handle_coordinator_update(self): def _determine_native_value(self): """Determine native value.""" return getattr(self.coordinator.data, self._property) - # TODO: add type handling - if self.entity_description.parent_key is not None: - native_value = self._extract_value_from_attribute( - getattr(self.coordinator.data, self.entity_description.parent_key), - self.entity_description.key, - ) - else: - native_value = self._extract_value_from_attribute( - self.coordinator.data, self.entity_description.key - ) - - if ( - self.device_class == SensorDeviceClass.TIMESTAMP - and native_value is not None - and (native_datetime := dt_util.parse_datetime(str(native_value))) - is not None - ): - return native_datetime.astimezone(dt_util.UTC) - - return native_value + # TODO: add type handling # pylint: disable=fixme + # if self.entity_description.parent_key is not None: + # native_value = self._extract_value_from_attribute( + # getattr(self.coordinator.data, self.entity_description.parent_key), + # self.entity_description.key, + # ) + # else: + # native_value = self._extract_value_from_attribute( + # self.coordinator.data, self.entity_description.key + # ) + + # if ( + # self.device_class == SensorDeviceClass.TIMESTAMP + # and native_value is not None + # and (native_datetime := dt_util.parse_datetime(str(native_value))) + # is not None + # ): + # return native_datetime.astimezone(dt_util.UTC) + + # return native_value diff --git a/homeassistant/components/xiaomi_miio/ng_switch.py b/homeassistant/components/xiaomi_miio/ng_switch.py index 90e75856522785..12465641e50d52 100644 --- a/homeassistant/components/xiaomi_miio/ng_switch.py +++ b/homeassistant/components/xiaomi_miio/ng_switch.py @@ -4,8 +4,10 @@ import logging from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription -from homeassistant.components.xiaomi_miio.device import XiaomiCoordinatedMiioEntity from homeassistant.core import callback +from homeassistant.helpers.entity import EntityCategory + +from .device import XiaomiCoordinatedMiioEntity _LOGGER = logging.getLogger(__name__) @@ -25,12 +27,16 @@ def __init__(self, device, switch, entry, coordinator): super().__init__(device, entry, unique_id, coordinator) + entity_category = None + if "entity_category" in switch.extras: + entity_category = EntityCategory(switch.extras.get("entity_category")) + description = SwitchEntityDescription( key=switch.id, name=name, icon=switch.extras.get("icon"), device_class=switch.extras.get("device_class"), - entity_category=switch.extras.get("entity_category"), + entity_category=entity_category, ) _LOGGER.debug("Adding switch: %s", description)