Skip to content

Commit

Permalink
Run formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Nov 7, 2023
1 parent b8ead0e commit 5265857
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 45 deletions.
2 changes: 1 addition & 1 deletion custom_components/landroid_cloud/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ".{}"
Expand Down
39 changes: 26 additions & 13 deletions custom_components/landroid_cloud/device_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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__()

Expand All @@ -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}"
Expand Down Expand Up @@ -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

Expand All @@ -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
pass
86 changes: 55 additions & 31 deletions custom_components/landroid_cloud/sensor.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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,
Expand Down

0 comments on commit 5265857

Please sign in to comment.