From 7fdbc7b75d01fb64b7f0e8e5b67b80a4e214005c Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Mon, 3 Jul 2023 04:43:14 +0200 Subject: [PATCH] Clean up solarlog const file (#95542) Move platform specifics to their own file --- homeassistant/components/solarlog/const.py | 196 ------------------- homeassistant/components/solarlog/sensor.py | 199 +++++++++++++++++++- 2 files changed, 197 insertions(+), 198 deletions(-) diff --git a/homeassistant/components/solarlog/const.py b/homeassistant/components/solarlog/const.py index 059dab3da78..d8ba49adbec 100644 --- a/homeassistant/components/solarlog/const.py +++ b/homeassistant/components/solarlog/const.py @@ -1,204 +1,8 @@ """Constants for the Solar-Log integration.""" from __future__ import annotations -from collections.abc import Callable -from dataclasses import dataclass -from datetime import datetime - -from homeassistant.components.sensor import ( - SensorDeviceClass, - SensorEntityDescription, - SensorStateClass, -) -from homeassistant.const import ( - PERCENTAGE, - UnitOfElectricPotential, - UnitOfEnergy, - UnitOfPower, -) -from homeassistant.util.dt import as_local - DOMAIN = "solarlog" # Default config for solarlog. DEFAULT_HOST = "http://solar-log" DEFAULT_NAME = "solarlog" - - -@dataclass -class SolarLogSensorEntityDescription(SensorEntityDescription): - """Describes Solarlog sensor entity.""" - - value: Callable[[float | int], float] | Callable[[datetime], datetime] | None = None - - -SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( - SolarLogSensorEntityDescription( - key="time", - name="last update", - device_class=SensorDeviceClass.TIMESTAMP, - value=as_local, - ), - SolarLogSensorEntityDescription( - key="power_ac", - name="power AC", - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfPower.WATT, - device_class=SensorDeviceClass.POWER, - state_class=SensorStateClass.MEASUREMENT, - ), - SolarLogSensorEntityDescription( - key="power_dc", - name="power DC", - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfPower.WATT, - device_class=SensorDeviceClass.POWER, - state_class=SensorStateClass.MEASUREMENT, - ), - SolarLogSensorEntityDescription( - key="voltage_ac", - name="voltage AC", - native_unit_of_measurement=UnitOfElectricPotential.VOLT, - device_class=SensorDeviceClass.VOLTAGE, - state_class=SensorStateClass.MEASUREMENT, - ), - SolarLogSensorEntityDescription( - key="voltage_dc", - name="voltage DC", - native_unit_of_measurement=UnitOfElectricPotential.VOLT, - device_class=SensorDeviceClass.VOLTAGE, - state_class=SensorStateClass.MEASUREMENT, - ), - SolarLogSensorEntityDescription( - key="yield_day", - name="yield day", - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), - ), - SolarLogSensorEntityDescription( - key="yield_yesterday", - name="yield yesterday", - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), - ), - SolarLogSensorEntityDescription( - key="yield_month", - name="yield month", - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), - ), - SolarLogSensorEntityDescription( - key="yield_year", - name="yield year", - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), - ), - SolarLogSensorEntityDescription( - key="yield_total", - name="yield total", - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - state_class=SensorStateClass.TOTAL, - value=lambda value: round(value / 1000, 3), - ), - SolarLogSensorEntityDescription( - key="consumption_ac", - name="consumption AC", - native_unit_of_measurement=UnitOfPower.WATT, - device_class=SensorDeviceClass.POWER, - state_class=SensorStateClass.MEASUREMENT, - ), - SolarLogSensorEntityDescription( - key="consumption_day", - name="consumption day", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), - ), - SolarLogSensorEntityDescription( - key="consumption_yesterday", - name="consumption yesterday", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), - ), - SolarLogSensorEntityDescription( - key="consumption_month", - name="consumption month", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), - ), - SolarLogSensorEntityDescription( - key="consumption_year", - name="consumption year", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), - ), - SolarLogSensorEntityDescription( - key="consumption_total", - name="consumption total", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - state_class=SensorStateClass.TOTAL, - value=lambda value: round(value / 1000, 3), - ), - SolarLogSensorEntityDescription( - key="total_power", - name="installed peak power", - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfPower.WATT, - device_class=SensorDeviceClass.POWER, - ), - SolarLogSensorEntityDescription( - key="alternator_loss", - name="alternator loss", - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfPower.WATT, - device_class=SensorDeviceClass.POWER, - state_class=SensorStateClass.MEASUREMENT, - ), - SolarLogSensorEntityDescription( - key="capacity", - name="capacity", - icon="mdi:solar-power", - native_unit_of_measurement=PERCENTAGE, - device_class=SensorDeviceClass.POWER_FACTOR, - state_class=SensorStateClass.MEASUREMENT, - value=lambda value: round(value * 100, 1), - ), - SolarLogSensorEntityDescription( - key="efficiency", - name="efficiency", - native_unit_of_measurement=PERCENTAGE, - device_class=SensorDeviceClass.POWER_FACTOR, - state_class=SensorStateClass.MEASUREMENT, - value=lambda value: round(value * 100, 1), - ), - SolarLogSensorEntityDescription( - key="power_available", - name="power available", - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfPower.WATT, - device_class=SensorDeviceClass.POWER, - state_class=SensorStateClass.MEASUREMENT, - ), - SolarLogSensorEntityDescription( - key="usage", - name="usage", - native_unit_of_measurement=PERCENTAGE, - device_class=SensorDeviceClass.POWER_FACTOR, - state_class=SensorStateClass.MEASUREMENT, - value=lambda value: round(value * 100, 1), - ), -) diff --git a/homeassistant/components/solarlog/sensor.py b/homeassistant/components/solarlog/sensor.py index 4180d48cdef..906d9aee629 100644 --- a/homeassistant/components/solarlog/sensor.py +++ b/homeassistant/components/solarlog/sensor.py @@ -1,13 +1,208 @@ """Platform for solarlog sensors.""" -from homeassistant.components.sensor import SensorEntity +from collections.abc import Callable +from dataclasses import dataclass +from datetime import datetime + +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorEntity, + SensorEntityDescription, + SensorStateClass, +) from homeassistant.config_entries import ConfigEntry +from homeassistant.const import ( + PERCENTAGE, + UnitOfElectricPotential, + UnitOfEnergy, + UnitOfPower, +) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity +from homeassistant.util.dt import as_local from . import SolarlogData -from .const import DOMAIN, SENSOR_TYPES, SolarLogSensorEntityDescription +from .const import DOMAIN + + +@dataclass +class SolarLogSensorEntityDescription(SensorEntityDescription): + """Describes Solarlog sensor entity.""" + + value: Callable[[float | int], float] | Callable[[datetime], datetime] | None = None + + +SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( + SolarLogSensorEntityDescription( + key="time", + name="last update", + device_class=SensorDeviceClass.TIMESTAMP, + value=as_local, + ), + SolarLogSensorEntityDescription( + key="power_ac", + name="power AC", + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfPower.WATT, + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + ), + SolarLogSensorEntityDescription( + key="power_dc", + name="power DC", + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfPower.WATT, + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + ), + SolarLogSensorEntityDescription( + key="voltage_ac", + name="voltage AC", + native_unit_of_measurement=UnitOfElectricPotential.VOLT, + device_class=SensorDeviceClass.VOLTAGE, + state_class=SensorStateClass.MEASUREMENT, + ), + SolarLogSensorEntityDescription( + key="voltage_dc", + name="voltage DC", + native_unit_of_measurement=UnitOfElectricPotential.VOLT, + device_class=SensorDeviceClass.VOLTAGE, + state_class=SensorStateClass.MEASUREMENT, + ), + SolarLogSensorEntityDescription( + key="yield_day", + name="yield day", + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), + ), + SolarLogSensorEntityDescription( + key="yield_yesterday", + name="yield yesterday", + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), + ), + SolarLogSensorEntityDescription( + key="yield_month", + name="yield month", + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), + ), + SolarLogSensorEntityDescription( + key="yield_year", + name="yield year", + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), + ), + SolarLogSensorEntityDescription( + key="yield_total", + name="yield total", + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL, + value=lambda value: round(value / 1000, 3), + ), + SolarLogSensorEntityDescription( + key="consumption_ac", + name="consumption AC", + native_unit_of_measurement=UnitOfPower.WATT, + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + ), + SolarLogSensorEntityDescription( + key="consumption_day", + name="consumption day", + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), + ), + SolarLogSensorEntityDescription( + key="consumption_yesterday", + name="consumption yesterday", + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), + ), + SolarLogSensorEntityDescription( + key="consumption_month", + name="consumption month", + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), + ), + SolarLogSensorEntityDescription( + key="consumption_year", + name="consumption year", + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + value=lambda value: round(value / 1000, 3), + ), + SolarLogSensorEntityDescription( + key="consumption_total", + name="consumption total", + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL, + value=lambda value: round(value / 1000, 3), + ), + SolarLogSensorEntityDescription( + key="total_power", + name="installed peak power", + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfPower.WATT, + device_class=SensorDeviceClass.POWER, + ), + SolarLogSensorEntityDescription( + key="alternator_loss", + name="alternator loss", + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfPower.WATT, + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + ), + SolarLogSensorEntityDescription( + key="capacity", + name="capacity", + icon="mdi:solar-power", + native_unit_of_measurement=PERCENTAGE, + device_class=SensorDeviceClass.POWER_FACTOR, + state_class=SensorStateClass.MEASUREMENT, + value=lambda value: round(value * 100, 1), + ), + SolarLogSensorEntityDescription( + key="efficiency", + name="efficiency", + native_unit_of_measurement=PERCENTAGE, + device_class=SensorDeviceClass.POWER_FACTOR, + state_class=SensorStateClass.MEASUREMENT, + value=lambda value: round(value * 100, 1), + ), + SolarLogSensorEntityDescription( + key="power_available", + name="power available", + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfPower.WATT, + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + ), + SolarLogSensorEntityDescription( + key="usage", + name="usage", + native_unit_of_measurement=PERCENTAGE, + device_class=SensorDeviceClass.POWER_FACTOR, + state_class=SensorStateClass.MEASUREMENT, + value=lambda value: round(value * 100, 1), + ), +) async def async_setup_entry(