Skip to content

Commit

Permalink
Implement time* handling, enabled_default, fix property attribute name (
Browse files Browse the repository at this point in the history
#4)

* Update ng_sensor.py

* Update ng_binary_sensor.py

* Update ng_switch.py

* Update ng_select.py

* Update ng_sensor.py
  • Loading branch information
starkillerOG authored Nov 15, 2022
1 parent 1365ed7 commit 7184cea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/xiaomi_miio/ng_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, device, sensor, entry, coordinator):
icon=sensor.extras.get("icon"),
device_class=sensor.extras.get("device_class"),
entity_category=category,
entity_registry_enabled_default=sensor.extras.get("enabled_default", True),
)

self.entity_description = description
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/xiaomi_miio/ng_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, device, setting, entry, coordinator):
# TODO: This should always be CONFIG for settables and non-configurable?
category = EntityCategory(setting.extras.get("entity_category", "config"))
self.entity_description = SelectEntityDescription(
key=setting.id,
key=setting.property,
name=setting.name,
icon=setting.extras.get("icon"),
device_class=setting.extras.get("device_class"),
Expand Down
30 changes: 23 additions & 7 deletions homeassistant/components/xiaomi_miio/ng_sensor.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
"""Support for Xiaomi Miio sensor entities."""
from __future__ import annotations
import datetime
from enum import Enum

from enum import Enum
import logging

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.components.xiaomi_miio.device import XiaomiMiioEntity
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.util import dt as dt_util

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -41,6 +48,7 @@ def __init__(
device_class=sensor.extras.get("device_class"),
state_class=sensor.extras.get("state_class"),
entity_category=category,
entity_registry_enabled_default=sensor.extras.get("enabled_default", True),
)
_LOGGER.debug("Adding sensor: %s", description)
super().__init__(device, entry, unique_id, coordinator)
Expand All @@ -64,11 +72,19 @@ def _determine_native_value(self):
val = getattr(self.coordinator.data, self._property)

if isinstance(val, Enum):
val = val.name

# TODO: check how to handle timestamps properly
# if(self.device_class == SensorDeviceClass.TIMESTAMP): ...
# native_dt = dt_util.parse_datetime(val)
# return native_dt.astimezone(dt_util.UTC)
return val.name
if (
self.device_class == SensorDeviceClass.TIMESTAMP
and val is not None
and (native_datetime := dt_util.parse_datetime(str(val)))
is not None
):
return native_datetime.astimezone(dt_util.UTC)
if isinstance(val, datetime.timedelta):
return self._parse_time_delta(val)
if isinstance(val, datetime.time):
return self._parse_datetime_time(val)
if isinstance(val, datetime.datetime):
return self._parse_datetime_datetime(val)

return val
2 changes: 1 addition & 1 deletion homeassistant/components/xiaomi_miio/ng_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, device, setting, entry, coordinator):
# TODO: This should always be CONFIG for settables and non-configurable?
category = EntityCategory(setting.extras.get("entity_category", "config"))
description = SwitchEntityDescription(
key=setting.id,
key=setting.property,
name=name,
icon=setting.extras.get("icon"),
device_class=setting.extras.get("device_class"),
Expand Down

0 comments on commit 7184cea

Please sign in to comment.