From 5265857165d402177c2d289e2f8403f9416aee7a Mon Sep 17 00:00:00 2001 From: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 7 Nov 2023 20:26:11 +0000 Subject: [PATCH] Run formatting --- custom_components/landroid_cloud/const.py | 2 +- .../landroid_cloud/device_base.py | 39 ++++++--- custom_components/landroid_cloud/sensor.py | 86 ++++++++++++------- 3 files changed, 82 insertions(+), 45 deletions(-) diff --git a/custom_components/landroid_cloud/const.py b/custom_components/landroid_cloud/const.py index acc0d1f..d1ef6f6 100644 --- a/custom_components/landroid_cloud/const.py +++ b/custom_components/landroid_cloud/const.py @@ -29,7 +29,7 @@ DEFAULT_NAME = "landroid" DOMAIN = "landroid_cloud" PLATFORMS_SECONDARY = [] -PLATFORMS_PRIMARY = ["lawn_mower","sensor"] +PLATFORMS_PRIMARY = ["lawn_mower", "sensor"] UPDATE_SIGNAL = "landroid_cloud_update" LOGLEVEL = LogLevel.DEBUG ENTITY_ID_FORMAT = DOMAIN + ".{}" diff --git a/custom_components/landroid_cloud/device_base.py b/custom_components/landroid_cloud/device_base.py index 3bbac58..cc35789 100644 --- a/custom_components/landroid_cloud/device_base.py +++ b/custom_components/landroid_cloud/device_base.py @@ -2,27 +2,25 @@ # pylint: disable=unused-argument,too-many-instance-attributes,no-self-use from __future__ import annotations -from collections.abc import Callable -from dataclasses import dataclass - import asyncio import json import logging import time +from collections.abc import Callable +from dataclasses import dataclass from datetime import timedelta from functools import partial from typing import Any -from homeassistant.config_entries import ConfigEntry from homeassistant.components.button import ButtonEntity, ButtonEntityDescription from homeassistant.components.lawn_mower import ( LawnMowerActivity, LawnMowerEntity, LawnMowerEntityFeature, ) -from homeassistant.components.sensor import SensorEntity from homeassistant.components.select import SelectEntity, SelectEntityDescription -from homeassistant.components.sensor import SensorEntityDescription +from homeassistant.components.sensor import SensorEntity, SensorEntityDescription +from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr @@ -905,12 +903,14 @@ async def async_config(self, data: dict | None = None) -> None: partial(self.api.cloud.send, device.serial_number, data) ) + @dataclass class LandroidBaseEntityDescriptionMixin: """Describes a basic Landroid entity.""" value_fn: Callable[[WorxCloud], bool | str | int | float] + @dataclass class LandroidSensorEntityDescription( SensorEntityDescription, LandroidBaseEntityDescriptionMixin @@ -921,12 +921,19 @@ class LandroidSensorEntityDescription( attributes: [] | None = None min_check_value: bool | int | float | str | None = None + class LandroidSensor(SensorEntity, LandroidLogger): """Representation of a Landroid sensor.""" _attr_has_entity_name = True - def __init__(self,hass:HomeAssistant, description:LandroidSensorEntityDescription, api:LandroidAPI, config:ConfigEntry)->None: + def __init__( + self, + hass: HomeAssistant, + description: LandroidSensorEntityDescription, + api: LandroidAPI, + config: ConfigEntry, + ) -> None: """Initialize a Landroid sensor.""" super().__init__() @@ -939,7 +946,9 @@ def __init__(self,hass:HomeAssistant, description:LandroidSensorEntityDescriptio self._attr_name = self.entity_description.name - _LOGGER.debug("(%s, Setup) Added sensor '%s'", self._api.friendly_name, self._attr_name) + _LOGGER.debug( + "(%s, Setup) Added sensor '%s'", self._api.friendly_name, self._attr_name + ) self._attr_unique_id = util_slugify( f"{self._attr_name}_{self._config.entry_id}" @@ -980,9 +989,7 @@ async def handle_update(self) -> None: new_attrib = {} write = False try: - new_val = self.entity_description.value_fn( - self.device - ) + new_val = self.entity_description.value_fn(self.device) except AttributeError: new_val = None @@ -1009,8 +1016,14 @@ async def handle_update(self) -> None: self._attr_extra_state_attributes = new_attrib if write: - _LOGGER.debug("(%s, Update signal) Updating sensor '%s' to new value '%s' with attributes '%s'", self._api.friendly_name, self._attr_name, self._attr_native_value, self._attr_extra_state_attributes) + _LOGGER.debug( + "(%s, Update signal) Updating sensor '%s' to new value '%s' with attributes '%s'", + self._api.friendly_name, + self._attr_name, + self._attr_native_value, + self._attr_extra_state_attributes, + ) try: self.async_write_ha_state() except: - pass \ No newline at end of file + pass diff --git a/custom_components/landroid_cloud/sensor.py b/custom_components/landroid_cloud/sensor.py index 5239878..efdf4f9 100644 --- a/custom_components/landroid_cloud/sensor.py +++ b/custom_components/landroid_cloud/sensor.py @@ -1,20 +1,17 @@ """Sensors for landroid_cloud.""" from __future__ import annotations -from datetime import datetime + import math +from datetime import datetime +from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant -from homeassistant.components.sensor import ( - SensorDeviceClass, - SensorStateClass, -) - -from .device_base import LandroidSensor, LandroidSensorEntityDescription from .api import LandroidAPI from .const import ATTR_DEVICES, DOMAIN +from .device_base import LandroidSensor, LandroidSensorEntityDescription SENSORS = [ LandroidSensorEntityDescription( @@ -25,8 +22,10 @@ device_class=SensorDeviceClass.BATTERY, entity_registry_enabled_default=True, native_unit_of_measurement="%", - value_fn=lambda landroid: landroid.battery["percent"] if "percent" in landroid.battery else None, - attributes=["charging"] + value_fn=lambda landroid: landroid.battery["percent"] + if "percent" in landroid.battery + else None, + attributes=["charging"], ), LandroidSensorEntityDescription( key="battery_temperature", @@ -36,7 +35,9 @@ device_class=SensorDeviceClass.TEMPERATURE, entity_registry_enabled_default=False, native_unit_of_measurement="°C", - value_fn=lambda landroid: landroid.battery["temperature"] if "temperature" in landroid.battery else None, + value_fn=lambda landroid: landroid.battery["temperature"] + if "temperature" in landroid.battery + else None, ), LandroidSensorEntityDescription( key="battery_cycles_total", @@ -46,8 +47,10 @@ device_class=None, entity_registry_enabled_default=False, native_unit_of_measurement=" ", - value_fn=lambda landroid: landroid.battery["cycles"]["total"] if "cycles" in landroid.battery else None, - icon="mdi:battery-sync" + value_fn=lambda landroid: landroid.battery["cycles"]["total"] + if "cycles" in landroid.battery + else None, + icon="mdi:battery-sync", ), LandroidSensorEntityDescription( key="battery_voltage", @@ -57,7 +60,9 @@ device_class=SensorDeviceClass.VOLTAGE, entity_registry_enabled_default=False, native_unit_of_measurement="V", - value_fn=lambda landroid: landroid.battery["voltage"] if "voltage" in landroid.battery else None, + value_fn=lambda landroid: landroid.battery["voltage"] + if "voltage" in landroid.battery + else None, ), LandroidSensorEntityDescription( key="blades_total_on", @@ -68,8 +73,10 @@ entity_registry_enabled_default=False, native_unit_of_measurement="hours", suggested_display_precision=0, - value_fn=lambda landroid: round(landroid.blades["total_on"] / 60, 0) if "total_on" in landroid.blades else None, - icon="mdi:saw-blade" + value_fn=lambda landroid: round(landroid.blades["total_on"] / 60, 0) + if "total_on" in landroid.blades + else None, + icon="mdi:saw-blade", ), LandroidSensorEntityDescription( key="blades_current_on", @@ -80,8 +87,10 @@ entity_registry_enabled_default=False, native_unit_of_measurement="hours", suggested_display_precision=0, - value_fn=lambda landroid: round(landroid.blades["current_on"] / 60, 0) if "current_on" in landroid.blades else None, - icon="mdi:saw-blade" + value_fn=lambda landroid: round(landroid.blades["current_on"] / 60, 0) + if "current_on" in landroid.blades + else None, + icon="mdi:saw-blade", ), LandroidSensorEntityDescription( key="blades_reset_at", @@ -92,8 +101,10 @@ entity_registry_enabled_default=False, native_unit_of_measurement="hours", suggested_display_precision=0, - value_fn=lambda landroid: round(landroid.blades["reset_at"] / 60, 0) if "reset_at" in landroid.blades else None, - icon="mdi:history" + value_fn=lambda landroid: round(landroid.blades["reset_at"] / 60, 0) + if "reset_at" in landroid.blades + else None, + icon="mdi:history", ), LandroidSensorEntityDescription( key="blades_reset_time", @@ -103,7 +114,9 @@ device_class=SensorDeviceClass.TIMESTAMP, entity_registry_enabled_default=False, native_unit_of_measurement="", - value_fn=lambda landroid: landroid.blades["reset_time"] if "reset_time" in landroid.blades else None, + value_fn=lambda landroid: landroid.blades["reset_time"] + if "reset_time" in landroid.blades + else None, ), LandroidSensorEntityDescription( key="error", @@ -172,7 +185,9 @@ device_class=None, entity_registry_enabled_default=True, native_unit_of_measurement=None, - value_fn=lambda landroid: landroid.rainsensor["triggered"] if "triggered" in landroid.rainsensor else None, + value_fn=lambda landroid: landroid.rainsensor["triggered"] + if "triggered" in landroid.rainsensor + else None, icon="mdi:weather-rainy", ), LandroidSensorEntityDescription( @@ -183,7 +198,9 @@ device_class=None, entity_registry_enabled_default=True, native_unit_of_measurement="minutes", - value_fn=lambda landroid: landroid.rainsensor["delay"] if "delay" in landroid.rainsensor else None, + value_fn=lambda landroid: landroid.rainsensor["delay"] + if "delay" in landroid.rainsensor + else None, icon="mdi:weather-rainy", ), LandroidSensorEntityDescription( @@ -194,7 +211,9 @@ device_class=None, entity_registry_enabled_default=False, native_unit_of_measurement="minutes", - value_fn=lambda landroid: landroid.rainsensor["remaining"] if "remaining" in landroid.rainsensor else None, + value_fn=lambda landroid: landroid.rainsensor["remaining"] + if "remaining" in landroid.rainsensor + else None, icon="mdi:weather-rainy", ), LandroidSensorEntityDescription( @@ -206,7 +225,9 @@ entity_registry_enabled_default=False, native_unit_of_measurement="km", suggested_display_precision=0, - value_fn=lambda landroid: round(landroid.statistics["distance"] / 1000, 0) if "distance" in landroid.statistics else None, + value_fn=lambda landroid: round(landroid.statistics["distance"] / 1000, 0) + if "distance" in landroid.statistics + else None, ), LandroidSensorEntityDescription( key="worktime_total", @@ -217,8 +238,10 @@ entity_registry_enabled_default=False, native_unit_of_measurement="hours", suggested_display_precision=0, - value_fn=lambda landroid: round(landroid.statistics["worktime_total"] / 60, 0) if "worktime_total" in landroid.statistics else None, - icon="mdi:update" + value_fn=lambda landroid: round(landroid.statistics["worktime_total"] / 60, 0) + if "worktime_total" in landroid.statistics + else None, + icon="mdi:update", ), LandroidSensorEntityDescription( key="rssi", @@ -228,7 +251,7 @@ device_class=SensorDeviceClass.SIGNAL_STRENGTH, entity_registry_enabled_default=True, native_unit_of_measurement="dBm", - value_fn=lambda landroid: landroid.rssi + value_fn=lambda landroid: landroid.rssi, ), LandroidSensorEntityDescription( key="last_update", @@ -239,7 +262,7 @@ entity_registry_enabled_default=True, native_unit_of_measurement=None, value_fn=lambda landroid: datetime.fromisoformat(landroid.updated), - icon="mdi:clock-check" + icon="mdi:clock-check", ), LandroidSensorEntityDescription( key="next_start", @@ -251,7 +274,7 @@ native_unit_of_measurement=None, value_fn=lambda landroid: landroid.schedules["next_schedule_start"], icon="mdi:clock-start", - attributes=["schedule"] + attributes=["schedule"], ), LandroidSensorEntityDescription( key="daily_progress", @@ -262,10 +285,11 @@ entity_registry_enabled_default=False, native_unit_of_measurement="%", value_fn=lambda landroid: landroid.schedules["daily_progress"], - icon="mdi:progress-clock" - ) + icon="mdi:progress-clock", + ), ] + async def async_setup_entry( hass: HomeAssistant, config: ConfigEntry,