Skip to content

Commit

Permalink
✨ [2024-04-28] 添加新特性: 电池相关内容的代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
MapleEve committed Apr 28, 2024
1 parent 172843d commit ccaeda4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
18 changes: 13 additions & 5 deletions custom_components/lifesmart/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import asyncio

from homeassistant.components.cover import (
ENTITY_ID_FORMAT,
ATTR_POSITION,
CoverEntity,
CoverEntityFeature,
)
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo

from . import LifeSmartDevice
from . import LifeSmartDevice, generate_entity_id
from .const import (
DOMAIN,
DEVICE_ID_KEY,
Expand Down Expand Up @@ -84,9 +83,7 @@ def __init__(self, device, raw_device_data, idx, val, client):
self.device_type = device_type
self.raw_device_data = raw_device_data
self._device = device
self.entity_id = ENTITY_ID_FORMAT.format(
(device_type + "_" + hub_id[:-3] + "_" + device_id).lower()
)
self.entity_id = generate_entity_id(device_type, hub_id, device_id, idx)
self._client = client

self._device_class = "curtain"
Expand All @@ -113,6 +110,7 @@ def __init__(self, device, raw_device_data, idx, val, client):
self._open_cmd = {"type": "0x81", "val": 1, "idx": "P2"}
self._close_cmd = {"type": "0x81", "val": 1, "idx": "P3"}
self._stop_cmd = {"type": "0x81", "val": 1, "idx": "P4"}
self._attr_battery_level = val["P8"]["v"]
elif device_type == "SL_SW_WIN":
self._open_cmd = {"type": "0x81", "val": 1, "idx": "OP"}
self._close_cmd = {"type": "0x81", "val": 1, "idx": "CL"}
Expand Down Expand Up @@ -194,6 +192,15 @@ async def async_stop_cover(self, **kwargs):
self._is_opening = False
self.schedule_update_ha_state()

async def async_toggle(self, **kwargs):
"""Toggle the entity."""
if self.is_opening or self.is_closing:
await self.async_stop_cover()
elif self.is_closed:
await self.async_open_cover()
else:
await self.async_close_cover()

async def async_set_cover_position(self, **kwargs):
"""Move the cover to a specific position."""
if not self._supported_features & CoverEntityFeature.SET_POSITION:
Expand Down Expand Up @@ -250,6 +257,7 @@ async def _update_state(self, data) -> None:
elif self.device_type == "SL_P_V2":
for idx in self._idx:
self._pos[idx] = data[idx]["val"]
self._attr_battery_level = data["P8"]["v"]
self._state = self._pos["P2"]
if data["P2"]["type"] & 0x01 == 1:
self._is_opening = True
Expand Down
15 changes: 7 additions & 8 deletions custom_components/lifesmart/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import logging

from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.const import (
PERCENTAGE,
UnitOfTemperature,
Expand All @@ -18,6 +17,7 @@

from . import LifeSmartDevice, generate_entity_id
from .const import (
COVER_TYPES,
DEVICE_DATA_KEY,
DEVICE_ID_KEY,
DEVICE_NAME_KEY,
Expand Down Expand Up @@ -148,8 +148,6 @@ def __init__(
)
self._client = client

# devtype = raw_device_data["devtype"]

if device_type in GAS_SENSOR_TYPES:
self._device_class = SensorDeviceClass.GAS
self._unit = "None"
Expand All @@ -172,7 +170,11 @@ def __init__(
elif sub_device_key == "Z":
self._device_class = SensorDeviceClass.ILLUMINANCE
self._unit = LIGHT_LUX
elif sub_device_key == "V":
elif (
sub_device_key == "V"
or sub_device_key == "BAT"
or (sub_device_key == "P8" and device_type in COVER_TYPES)
):
self._device_class = SensorDeviceClass.BATTERY
self._unit = PERCENTAGE
elif sub_device_key == "P3":
Expand All @@ -181,9 +183,6 @@ def __init__(
elif sub_device_key == "P4":
self._device_class = "None"
self._unit = CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER
elif sub_device_key == "BAT":
self._device_class = SensorDeviceClass.BATTERY
self._unit = PERCENTAGE
else:
self._unit = "None"
self._device_class = "None"
Expand Down

0 comments on commit ccaeda4

Please sign in to comment.