Skip to content

Commit

Permalink
Add default entity categories
Browse files Browse the repository at this point in the history
Settings & actions default to config, sensors to diagnostic
  • Loading branch information
rytilahti committed Nov 9, 2022
1 parent 85b7d53 commit 1365ed7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
5 changes: 4 additions & 1 deletion homeassistant/components/xiaomi_miio/ng_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)
from homeassistant.components.xiaomi_miio.device import XiaomiMiioEntity
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory

_LOGGER = logging.getLogger(__name__)

Expand All @@ -27,12 +28,14 @@ def __init__(self, device, sensor, entry, coordinator):

super().__init__(device, entry, unique_id, coordinator)

# TODO: This should always be CONFIG for settables and non-configurable?
category = EntityCategory(sensor.extras.get("entity_category", "diagnostic"))
description = BinarySensorEntityDescription(
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=category,
)

self.entity_description = description
Expand Down
15 changes: 11 additions & 4 deletions homeassistant/components/xiaomi_miio/ng_button.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
"""Support for Xiaomi Miio button entities."""
from __future__ import annotations

from typing import Callable

from homeassistant.components.button import (
ButtonDeviceClass,
ButtonEntity,
ButtonEntityDescription,
)
from homeassistant.components.xiaomi_miio.device import XiaomiMiioEntity
from homeassistant.util import slugify
from homeassistant.helpers.entity import EntityCategory


class XiaomiButton(XiaomiMiioEntity, ButtonEntity):
"""Representation of Xiaomi button."""

entity_description: ButtonEntityDescription
method: callable
method: Callable

_attr_device_class = ButtonDeviceClass.RESTART
_attr_has_entity_name = True

def __init__(self, button, device, entry, coordinator):
"""Initialize the plug switch."""
self._name = name = button.name
self._name = button.name
unique_id = f"{entry.unique_id}_button_{button.id}"
self.method = button.method

super().__init__(device, entry, unique_id, coordinator)

# TODO: This should always be CONFIG for settables and non-configurable?
category = EntityCategory(button.extras.get("entity_category", "config"))
description = ButtonEntityDescription(
key=button.id,
name=button.name,
icon=button.extras.get("icon"),
device_class=button.extras.get("device_class"),
entity_category=button.extras.get("entity_category"),
entity_category=category,
)

self.entity_description = description
Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/xiaomi_miio/ng_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from homeassistant.components.number import NumberEntity, NumberEntityDescription
from homeassistant.components.xiaomi_miio.device import XiaomiMiioEntity
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory


class XiaomiNumber(XiaomiMiioEntity, NumberEntity):
Expand All @@ -20,12 +21,15 @@ def __init__(self, device, setting, entry, coordinator):
self._attr_native_value = self._extract_value_from_attribute(
coordinator.data, setting.id
)

# TODO: This should always be CONFIG for settables and non-configurable?
category = EntityCategory(setting.extras.get("entity_category", "config"))
description = NumberEntityDescription(
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=category,
native_unit_of_measurement=setting.unit,
native_min_value=setting.min_value,
native_max_value=setting.max_value,
Expand Down
6 changes: 4 additions & 2 deletions homeassistant/components/xiaomi_miio/ng_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.components.xiaomi_miio.device import XiaomiMiioEntity
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory

_LOGGER = logging.getLogger(__name__)

Expand All @@ -27,13 +28,14 @@ def __init__(self, device, setting, entry, coordinator):
None # TODO we don't know the value, but the parent wants it?
)

# 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,
name=setting.name,
icon=setting.extras.get("icon"),
device_class=setting.extras.get("device_class"),
entity_category=setting.extras.get("entity_category"),
# entity_category=EntityCategory.CONFIG,
entity_category=category,
)
self._attr_options = [x.name for x in self._choices]

Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/xiaomi_miio/ng_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.components.xiaomi_miio.device import XiaomiMiioEntity
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory

_LOGGER = logging.getLogger(__name__)

Expand All @@ -30,14 +31,16 @@ def __init__(

unique_id = f"{entry.unique_id}_sensor_{sensor.id}"

# TODO: This should always be CONFIG for settables and non-configurable?
category = EntityCategory(sensor.extras.get("entity_category", "diagnostic"))
description = SensorEntityDescription(
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=category,
)
_LOGGER.debug("Adding sensor: %s", description)
super().__init__(device, entry, unique_id, coordinator)
Expand Down
23 changes: 13 additions & 10 deletions homeassistant/components/xiaomi_miio/ng_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,34 @@
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.components.xiaomi_miio.device import XiaomiMiioEntity
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory

_LOGGER = logging.getLogger(__name__)


class XiaomiSwitch(XiaomiMiioEntity, SwitchEntity):
"""Representation of a Xiaomi Plug Generic."""
"""Representation of Xiaomi switch."""

entity_description: SwitchEntityDescription
_attr_has_entity_name = True

def __init__(self, device, switch, entry, coordinator):
def __init__(self, device, setting, entry, coordinator):
"""Initialize the plug switch."""
self._name = name = switch.name
self._property = switch.property
self._setter = switch.setter
unique_id = f"{entry.unique_id}_switch_{switch.id}"
self._name = name = setting.name
self._property = setting.property
self._setter = setting.setter
unique_id = f"{entry.unique_id}_switch_{setting.id}"

super().__init__(device, entry, unique_id, coordinator)

# TODO: This should always be CONFIG for settables and non-configurable?
category = EntityCategory(setting.extras.get("entity_category", "config"))
description = SwitchEntityDescription(
key=switch.id,
key=setting.id,
name=name,
icon=switch.extras.get("icon"),
device_class=switch.extras.get("device_class"),
entity_category=switch.extras.get("entity_category"),
icon=setting.extras.get("icon"),
device_class=setting.extras.get("device_class"),
entity_category=category,
)

_LOGGER.debug("Adding switch: %s", description)
Expand Down

0 comments on commit 1365ed7

Please sign in to comment.