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

Implement time* handling, enabled_default, fix property attribute name #4

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
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