Skip to content

Commit

Permalink
Add more sensors and translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre0512 committed Mar 5, 2023
1 parent ac9e4d2 commit 5682149
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 24 deletions.
86 changes: 86 additions & 0 deletions custom_components/hOn/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import logging
from dataclasses import dataclass

from pyhon import HonConnection

from homeassistant.components.binary_sensor import BinarySensorEntityDescription, BinarySensorDeviceClass, \
BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback
from .const import DOMAIN
from .hon import HonCoordinator, HonEntity

_LOGGER = logging.getLogger(__name__)


@dataclass
class HonBinarySensorEntityDescriptionMixin:
on_value: str = ""


@dataclass
class HonBinarySensorEntityDescription(HonBinarySensorEntityDescriptionMixin, BinarySensorEntityDescription):
pass


BINARY_SENSORS: dict[str, tuple[HonBinarySensorEntityDescription, ...]] = {
"WM": (
HonBinarySensorEntityDescription(
key="lastConnEvent.category",
name="Connection",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
on_value="CONNECTED",
),
HonBinarySensorEntityDescription(
key="doorLockStatus",
name="Door Locked",
device_class=BinarySensorDeviceClass.DOOR,
on_value="1",
),
)
}


async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: HonConnection = hass.data[DOMAIN][entry.unique_id]
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
for device in hon.devices:
if device.mac_address in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.mac_address]
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.mac_address] = coordinator
await coordinator.async_config_entry_first_refresh()

if descriptions := BINARY_SENSORS.get(device.appliance_type_name):
for description in descriptions:
if not device.data.get(description.key):
_LOGGER.info("Can't setup %s", description.key)
continue
appliances.extend([
HonBinarySensorEntity(hass, coordinator, entry, device, description)]
)

async_add_entities(appliances)


class HonBinarySensorEntity(HonEntity, BinarySensorEntity):
entity_description: HonBinarySensorEntityDescription

def __init__(self, hass, coordinator, entry, device, description) -> None:
super().__init__(hass, entry, coordinator, device)

self._coordinator = coordinator

self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}"

@property
def is_on(self) -> bool:
return self._device.data.get(self.entity_description.key, "") == self.entity_description.on_value

@callback
def _handle_coordinator_update(self):
self._attr_native_value = self._device.data.get(self.entity_description.key, "")
self.async_write_ha_state()
12 changes: 2 additions & 10 deletions custom_components/hOn/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@

BUTTONS: dict[str, tuple[ButtonEntityDescription, ...]] = {
"WM": (
ButtonEntityDescription(
key="startProgram",
name="Start Program",
icon="mdi:play",
),
ButtonEntityDescription(
key="stopProgram",
name="Stop Program",
icon="mdi:stop",
),
ButtonEntityDescription(
key="pauseProgram",
name="Pause Program",
Expand Down Expand Up @@ -47,6 +37,8 @@ async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> Non

if descriptions := BUTTONS.get(device.appliance_type_name):
for description in descriptions:
if not device.commands.get(description.key):
continue
appliances.extend([
HonButtonEntity(hass, coordinator, entry, device, description)]
)
Expand Down
3 changes: 2 additions & 1 deletion custom_components/hOn/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"select",
"number",
"switch",
"button"
"button",
"binary_sensor",
]
4 changes: 2 additions & 2 deletions custom_components/hOn/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"domain": "hon",
"name": "hOn",
"config_flow": true,
"version": "0.0.1",
"version": "0.1.0",
"codeowners": ["@Andre0512"],
"iot_class": "cloud_polling",
"requirements": ["pyhon"],
"requirements": ["pyhOn==0.2.4"],
"documentation": "https://github.com/Andre0512/hOn/"
}
3 changes: 2 additions & 1 deletion custom_components/hOn/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> Non

if descriptions := SELECTS.get(device.appliance_type_name):
for description in descriptions:
if not device.data.get(description.key):
continue
appliances.extend([
HonSelectEntity(hass, coordinator, entry, device, description)]
)

async_add_entities(appliances)


Expand Down
9 changes: 7 additions & 2 deletions custom_components/hOn/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,25 @@
),
SensorEntityDescription(
key="startProgram.weight",
name="Weight",
name="Suggested weight",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.CONFIG,
native_unit_of_measurement=UnitOfMass.KILOGRAMS,
icon="mdi:weight-kilogram"
),
SensorEntityDescription(
key="machMode",
name="Mode",
name="Machine Last Status",
icon="mdi:information",
translation_key="mode"
),
SensorEntityDescription(
key="errors",
name="Last Error",
icon="mdi:math-log",
translation_key="errors"
),

)
}

Expand All @@ -91,6 +94,8 @@ async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> Non

if descriptions := SENSORS.get(device.appliance_type_name):
for description in descriptions:
if not device.data.get(description.key):
continue
appliances.extend([
HonSensorEntity(hass, coordinator, entry, device, description)]
)
Expand Down
8 changes: 4 additions & 4 deletions custom_components/hOn/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> Non

if descriptions := SWITCHES.get(device.appliance_type_name):
for description in descriptions:
appliances.extend([
HonSwitchEntity(hass, coordinator, entry, device, description)]
)
if device.data.get(description.key) is not None or device.commands.get(description.key) is not None:
appliances.extend([
HonSwitchEntity(hass, coordinator, entry, device, description)]
)

async_add_entities(appliances)

Expand All @@ -85,7 +86,6 @@ def available(self) -> bool:
return self._device.settings[self.entity_description.key].typology == "fixed"
return True


@property
def is_on(self) -> bool | None:
"""Return True if entity is on."""
Expand Down
8 changes: 5 additions & 3 deletions custom_components/hOn/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
}
},
"errors": {
"00": "No error",
"100000000000": "E2: Check if the door is closed",
"8000000000000": "E4: Check the water supply"
"state": {
"00": "No error",
"100000000000": "E2: Check if the door is closed",
"8000000000000": "E4: Check the water supply"
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "Haier hOn",
"name": "hOn",
"render_readme": false,
"homeassistant": "2023.2.0"
}

0 comments on commit 5682149

Please sign in to comment.