Skip to content

Commit

Permalink
v1.1.7-dev3
Browse files Browse the repository at this point in the history
  • Loading branch information
lunDreame34 committed Nov 2, 2024
1 parent 9fac593 commit 469bbc8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 47 deletions.
16 changes: 8 additions & 8 deletions custom_components/bestin/center.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,28 +173,28 @@ async def handle_message_info(self, message: str):
if len(self.elevator_data) >= 2:
for idx, (serial, info) in enumerate(self.elevator_data.items(), start=1):
floor = info["Floor"]
move_dir = info["MoveDir"].upper()
move_dir = info["MoveDir"].capitalize()
self.elevator_data[f"floor{str(idx)}"] = floor

self.set_device("elevator", 1, f"floor_{str(idx)}", floor)
self.set_device("elevator", 1, f"direction_{str(idx)}", move_dir)
else:
floor = move_info["Floor"]
move_dir = move_info["MoveDir"].upper()
move_dir = move_info["MoveDir"].capitalize()
self.elevator_data[f"floor1"] = floor

self.set_device("elevator", 1, "floor_1", floor)
self.set_device("elevator", 1, "direction_1", move_dir)
else:
for idx in range(1, self.elevator_number + 1):
floor = self.elevator_data.get(f"floor{str(idx)}", "UNKNOWN")
floor = self.elevator_data.get(f"floor{str(idx)}", "Unknown")

self.set_device("elevator", 1, f"floor_{str(idx)}", floor)
self.set_device("elevator", 1, f"direction_{str(idx)}", "ARRIVAL")
self.set_device("elevator", 1, f"direction_{str(idx)}", "Arrival")
await asyncio.sleep(2) # Wait for a while

self.set_device("elevator", 1, f"floor_{str(idx)}", "")
self.set_device("elevator", 1, f"direction_{str(idx)}", "IDLE")
self.set_device("elevator", 1, f"floor_{str(idx)}", "-")
self.set_device("elevator", 1, f"direction_{str(idx)}", "Idle")

self.elevator_arrived = True

Expand Down Expand Up @@ -507,8 +507,8 @@ def result_after_request(self, response: dict | str) -> str:
def _elevator_registration(self, id: str):
"""Register elevator device."""
self.set_device("elevator", 1, id, False)
self.set_device("elevator", 1, f"floor_{id}", "")
self.set_device("elevator", 1, f"direction_{id}", "IDLE")
self.set_device("elevator", 1, f"floor_{id}", "-")
self.set_device("elevator", 1, f"direction_{id}", "Idle")

def _parse_common_status(
self, device_type: str, device_number: int, unit_num: str, unit_status: dict | str
Expand Down
66 changes: 27 additions & 39 deletions custom_components/bestin/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from homeassistant.components.sensor import (
DOMAIN as DOMAIN_SENSOR,
SensorEntity,
SensorDeviceClass
)
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -37,9 +38,9 @@
}

DEVICE_CLASS = {
"light:dcvalue": UnitOfPower.WATT,
"outlet:cutvalue": UnitOfPower.WATT,
"outlet:powercons": UnitOfPower.WATT,
"light:dcvalue": SensorDeviceClass.POWER,
"outlet:cutvalue": SensorDeviceClass.POWER,
"outlet:powercons": SensorDeviceClass.POWER,
"electric:realtime": SensorDeviceClass.POWER,
"electric:total": SensorDeviceClass.ENERGY,
"gas:total": SensorDeviceClass.GAS,
Expand All @@ -63,29 +64,20 @@
}

VALUE_CONVERSION = {
"electric:total": lambda value: round(value / 100, 2),
"electric:realtime": lambda value: value,
"gas:total": lambda value: round(value / 1000, 2),
"gas:realtime": lambda value: value / 10,
"heat:total": lambda value: round(value / 1000, 2),
"heat:realtime": [
lambda value: value,
lambda value: value / 1000
],
"hotwater:total": lambda value: round(value / 1000, 2),
"hotwater:realtime": [
lambda value: value,
lambda value: value / 1000
],
"water:total": lambda value: round(value / 1000, 2),
"water:realtime": [
lambda value: value,
lambda value: value / 1000,
],
"electric:total": lambda val, _: round(val / 100, 2),
"gas:total": lambda val, _: round(val / 1000, 2),
"gas:realtime": lambda val, _: val / 10,
"heat:total": lambda val, _: round(val / 1000, 2),
"heat:realtime": lambda val, wp_ver: val if wp_ver == "General" else val / 1000,
"hotwater:total": lambda val, _: round(val / 1000, 2),
"hotwater:realtime": lambda val, wp_ver: val if wp_ver == "General" else val / 1000,
"water:total": lambda val, _: round(val / 1000, 2),
"water:realtime": lambda val, wp_ver: val if wp_ver == "General" else val / 1000,
}


def extract_and_transform(identifier: str) -> str:
"""Extract and transform the identifier to a formatted string."""
if "energy_" in identifier:
extracted_segment = identifier.split("energy_")[1]
else:
Expand Down Expand Up @@ -126,41 +118,37 @@ def async_add_sensor(devices=None):
async_add_sensor()


class BestinSensor(BestinDevice):
class BestinSensor(BestinDevice, SensorEntity):
"""Defined the Sensor."""
TYPE = DOMAIN_SENSOR

def __init__(self, device, hub) -> None:
"""Initialize the sensor."""
super().__init__(device, hub)
self._attr_id = extract_and_transform(self._device_info.device_id)
self._attr_icon = DEVICE_ICON.get(self._attr_id, None)
self._attr_icon = DEVICE_ICON.get(self._attr_id)

@property
def state(self):
def native_value(self):
"""Return the state of the sensor."""
if self._attr_id in VALUE_CONVERSION:
factor = VALUE_CONVERSION[self._attr_id]

if isinstance(factor, list) and len(factor) == 2:
factor = factor[0] if self.hub.wp_version == "General" else factor[1]
return factor(self._device_info.state)
else:
return self._device_info.state
factor = VALUE_CONVERSION.get(self._attr_id)
if callable(factor):
return factor(self._device_info.state, self.hub.wp_version)
return self._device_info.state

@property
def device_class(self):
"""Return the class of the sensor."""
return DEVICE_CLASS.get(self._attr_id, None)
return DEVICE_CLASS.get(self._attr_id)

@property
def unit_of_measurement(self):
def native_unit_of_measurement(self):
"""Return the unit of measurement of this sensor."""
return DEVICE_UNIT.get(self._attr_id, None)
return DEVICE_UNIT.get(self._attr_id)

@property
def state_class(self):
"""Type of this sensor state."""
return (
"total_increasing" if "total" in self._attr_id else "measurement"
)
if self._device_info.device_type in ["light:dcvalue", "outlet:powercons", "energy"]:
return "total_increasing" if "total" in self._attr_id else "measurement"
return None

0 comments on commit 469bbc8

Please sign in to comment.