Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cast entity_category string to EntityCategory enum #3

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions homeassistant/components/xiaomi_miio/ng_binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions homeassistant/components/xiaomi_miio/ng_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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]
Expand Down
59 changes: 33 additions & 26 deletions homeassistant/components/xiaomi_miio/ng_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -35,14 +38,18 @@ 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,
native_unit_of_measurement=sensor.unit,
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)
Expand All @@ -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
10 changes: 8 additions & 2 deletions homeassistant/components/xiaomi_miio/ng_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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)
Expand Down