mirror of
https://github.com/home-assistant/core.git
synced 2025-08-01 09:38:21 +00:00
Mill, new features
Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
This commit is contained in:
parent
a927312fb5
commit
e07ca1a1bc
@ -14,6 +14,7 @@ from homeassistant.components.climate import (
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_TEMPERATURE,
|
||||
CONF_IP_ADDRESS,
|
||||
CONF_USERNAME,
|
||||
@ -29,6 +30,7 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
from .const import (
|
||||
ATTR_AWAY_TEMP,
|
||||
ATTR_COMFORT_TEMP,
|
||||
ATTR_MAX_HEATING_POWER,
|
||||
ATTR_ROOM_NAME,
|
||||
ATTR_SLEEP_TEMP,
|
||||
CLOUD,
|
||||
@ -38,6 +40,7 @@ from .const import (
|
||||
MANUFACTURER,
|
||||
MAX_TEMP,
|
||||
MIN_TEMP,
|
||||
SERVICE_MAX_HEATING_POWER,
|
||||
SERVICE_SET_ROOM_TEMP,
|
||||
)
|
||||
from .coordinator import MillDataUpdateCoordinator
|
||||
@ -50,6 +53,12 @@ SET_ROOM_TEMP_SCHEMA = vol.Schema(
|
||||
vol.Optional(ATTR_SLEEP_TEMP): cv.positive_int,
|
||||
}
|
||||
)
|
||||
LIMIT_HEATING_POWER_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(ATTR_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(ATTR_MAX_HEATING_POWER): vol.Range(min=0, max=2000),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
@ -84,6 +93,25 @@ async def async_setup_entry(
|
||||
DOMAIN, SERVICE_SET_ROOM_TEMP, set_room_temp, schema=SET_ROOM_TEMP_SCHEMA
|
||||
)
|
||||
|
||||
async def max_heating_power(service: ServiceCall) -> None:
|
||||
"""Limit heating power."""
|
||||
entity_id = service.data.get(ATTR_ENTITY_ID)
|
||||
heating_power = service.data.get(ATTR_MAX_HEATING_POWER)
|
||||
for entity in entities:
|
||||
if entity.entity_id == entity_id:
|
||||
await mill_data_coordinator.mill_data_connection.max_heating_power(
|
||||
entity.heater_id, heating_power
|
||||
)
|
||||
return
|
||||
raise ValueError(f"Entity id {entity_id} not found")
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_MAX_HEATING_POWER,
|
||||
max_heating_power,
|
||||
schema=LIMIT_HEATING_POWER_SCHEMA,
|
||||
)
|
||||
|
||||
|
||||
class MillHeater(CoordinatorEntity[MillDataUpdateCoordinator], ClimateEntity):
|
||||
"""Representation of a Mill Thermostat device."""
|
||||
@ -111,7 +139,7 @@ class MillHeater(CoordinatorEntity[MillDataUpdateCoordinator], ClimateEntity):
|
||||
|
||||
self._available = False
|
||||
|
||||
self._id = heater.device_id
|
||||
self.heater_id = heater.device_id
|
||||
self._attr_unique_id = heater.device_id
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, heater.device_id)},
|
||||
@ -127,7 +155,10 @@ class MillHeater(CoordinatorEntity[MillDataUpdateCoordinator], ClimateEntity):
|
||||
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
|
||||
return
|
||||
await self.coordinator.mill_data_connection.set_heater_temp(
|
||||
self._id, float(temperature)
|
||||
self.heater_id, float(temperature)
|
||||
)
|
||||
await self.coordinator.mill_data_connection.fetch_historic_energy_usage(
|
||||
self.heater_id
|
||||
)
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
@ -135,12 +166,12 @@ class MillHeater(CoordinatorEntity[MillDataUpdateCoordinator], ClimateEntity):
|
||||
"""Set new target hvac mode."""
|
||||
if hvac_mode == HVACMode.HEAT:
|
||||
await self.coordinator.mill_data_connection.heater_control(
|
||||
self._id, power_status=True
|
||||
self.heater_id, power_status=True
|
||||
)
|
||||
await self.coordinator.async_request_refresh()
|
||||
elif hvac_mode == HVACMode.OFF:
|
||||
await self.coordinator.mill_data_connection.heater_control(
|
||||
self._id, power_status=False
|
||||
self.heater_id, power_status=False
|
||||
)
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
@ -152,7 +183,7 @@ class MillHeater(CoordinatorEntity[MillDataUpdateCoordinator], ClimateEntity):
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
self._update_attr(self.coordinator.data[self._id])
|
||||
self._update_attr(self.coordinator.data[self.heater_id])
|
||||
self.async_write_ha_state()
|
||||
|
||||
@callback
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
ATTR_AWAY_TEMP = "away_temp"
|
||||
ATTR_COMFORT_TEMP = "comfort_temp"
|
||||
ATTR_MAX_HEATING_POWER = "max_heating_power"
|
||||
ATTR_ROOM_NAME = "room_name"
|
||||
ATTR_SLEEP_TEMP = "sleep_temp"
|
||||
BATTERY = "battery"
|
||||
@ -9,6 +10,8 @@ CLOUD = "Cloud"
|
||||
CONNECTION_TYPE = "connection_type"
|
||||
CONSUMPTION_TODAY = "day_consumption"
|
||||
CONSUMPTION_YEAR = "year_consumption"
|
||||
CONTROL_SIGNAL = "control_signal"
|
||||
CURRENT_POWER = "current_power"
|
||||
DOMAIN = "mill"
|
||||
ECO2 = "eco2"
|
||||
HUMIDITY = "humidity"
|
||||
@ -17,5 +20,6 @@ MANUFACTURER = "Mill"
|
||||
MAX_TEMP = 35
|
||||
MIN_TEMP = 5
|
||||
SERVICE_SET_ROOM_TEMP = "set_room_temperature"
|
||||
SERVICE_MAX_HEATING_POWER = "max_heating_power"
|
||||
TEMPERATURE = "current_temp"
|
||||
TVOC = "tvoc"
|
||||
|
@ -4,12 +4,22 @@ from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import cast
|
||||
|
||||
from mill import Mill
|
||||
from mill import Heater, Mill
|
||||
from mill_local import Mill as MillLocal
|
||||
|
||||
from homeassistant.components.recorder import get_instance
|
||||
from homeassistant.components.recorder.models import StatisticData, StatisticMetaData
|
||||
from homeassistant.components.recorder.statistics import (
|
||||
async_add_external_statistics,
|
||||
get_last_statistics,
|
||||
statistics_during_period,
|
||||
)
|
||||
from homeassistant.const import UnitOfEnergy
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
from homeassistant.util import dt as dt_util, slugify
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
@ -28,11 +38,94 @@ class MillDataUpdateCoordinator(DataUpdateCoordinator):
|
||||
) -> None:
|
||||
"""Initialize global Mill data updater."""
|
||||
self.mill_data_connection = mill_data_connection
|
||||
self._last_stats_time = dt_util.utcnow() - timedelta(days=1)
|
||||
|
||||
super().__init__(
|
||||
hass,
|
||||
_LOGGER,
|
||||
name=DOMAIN,
|
||||
update_method=mill_data_connection.fetch_heater_and_sensor_data,
|
||||
update_interval=update_interval,
|
||||
)
|
||||
|
||||
async def _async_update_data(self) -> dict:
|
||||
"""Update data via API."""
|
||||
data = await self.mill_data_connection.fetch_heater_and_sensor_data()
|
||||
if isinstance(self.mill_data_connection, Mill):
|
||||
await self._insert_statistics()
|
||||
return data
|
||||
|
||||
async def _insert_statistics(self) -> None:
|
||||
"""Insert Mill statistics."""
|
||||
now = dt_util.utcnow()
|
||||
if self._last_stats_time > now - timedelta(hours=1):
|
||||
return
|
||||
for dev_id, heater in self.mill_data_connection.devices.items():
|
||||
if not isinstance(heater, Heater):
|
||||
continue
|
||||
statistic_id = f"{DOMAIN}:energy_{slugify(dev_id)}_7"
|
||||
|
||||
last_stats = await get_instance(self.hass).async_add_executor_job(
|
||||
get_last_statistics, self.hass, 1, statistic_id, True, set()
|
||||
)
|
||||
|
||||
if not last_stats or not last_stats.get(statistic_id):
|
||||
hourly_data = (
|
||||
await self.mill_data_connection.fetch_historic_energy_usage(dev_id)
|
||||
)
|
||||
_sum = 0.0
|
||||
last_stats_time = None
|
||||
else:
|
||||
hourly_data = (
|
||||
await self.mill_data_connection.fetch_historic_energy_usage(
|
||||
dev_id,
|
||||
n_days=(
|
||||
now
|
||||
- dt_util.utc_from_timestamp(
|
||||
last_stats[statistic_id][0]["start"]
|
||||
)
|
||||
).days
|
||||
+ 1,
|
||||
)
|
||||
)
|
||||
if not hourly_data:
|
||||
return
|
||||
stats = await get_instance(self.hass).async_add_executor_job(
|
||||
statistics_during_period,
|
||||
self.hass,
|
||||
next(iter(hourly_data)),
|
||||
None,
|
||||
{statistic_id},
|
||||
"hour",
|
||||
None,
|
||||
{"sum", "state"},
|
||||
)
|
||||
stat = stats[statistic_id][0]
|
||||
|
||||
_sum = cast(float, stat["sum"])
|
||||
last_stats_time = dt_util.utc_from_timestamp(stat["start"])
|
||||
|
||||
statistics = []
|
||||
|
||||
for start, state in hourly_data.items():
|
||||
if state is None:
|
||||
continue
|
||||
if last_stats_time and (start < last_stats_time or start > now):
|
||||
continue
|
||||
_sum += state
|
||||
statistics.append(
|
||||
StatisticData(
|
||||
start=start,
|
||||
state=state,
|
||||
sum=_sum,
|
||||
)
|
||||
)
|
||||
metadata = StatisticMetaData(
|
||||
has_mean=False,
|
||||
has_sum=True,
|
||||
name=f"{heater.name}",
|
||||
source=DOMAIN,
|
||||
statistic_id=statistic_id,
|
||||
unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
||||
)
|
||||
async_add_external_statistics(self.hass, metadata, statistics)
|
||||
self._last_stats_time = now.replace(minute=0, second=0)
|
||||
|
@ -2,6 +2,9 @@
|
||||
"services": {
|
||||
"set_room_temperature": {
|
||||
"service": "mdi:thermometer"
|
||||
},
|
||||
"max_heating_power": {
|
||||
"service": "mdi:power"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,9 @@
|
||||
"name": "Mill",
|
||||
"codeowners": ["@danielhiversen"],
|
||||
"config_flow": true,
|
||||
"dependencies": ["recorder"],
|
||||
"documentation": "https://www.home-assistant.io/integrations/mill",
|
||||
"iot_class": "local_polling",
|
||||
"loggers": ["mill", "mill_local"],
|
||||
"requirements": ["millheater==0.11.8", "mill-local==0.3.0"]
|
||||
"requirements": ["millheater==0.12.0", "mill-local==0.3.0"]
|
||||
}
|
||||
|
@ -33,6 +33,8 @@ from .const import (
|
||||
CONNECTION_TYPE,
|
||||
CONSUMPTION_TODAY,
|
||||
CONSUMPTION_YEAR,
|
||||
CONTROL_SIGNAL,
|
||||
CURRENT_POWER,
|
||||
DOMAIN,
|
||||
ECO2,
|
||||
HUMIDITY,
|
||||
@ -57,6 +59,19 @@ HEATER_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key=CURRENT_POWER,
|
||||
translation_key="current_power",
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key=CONTROL_SIGNAL,
|
||||
translation_key="control_signal",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
)
|
||||
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
@ -94,6 +109,16 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
),
|
||||
)
|
||||
|
||||
SOCKET_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
key=HUMIDITY,
|
||||
device_class=SensorDeviceClass.HUMIDITY,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
*HEATER_SENSOR_TYPES,
|
||||
)
|
||||
|
||||
LOCAL_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
key="control_signal",
|
||||
@ -145,7 +170,9 @@ async def async_setup_entry(
|
||||
)
|
||||
for mill_device in mill_data_coordinator.data.values()
|
||||
for entity_description in (
|
||||
HEATER_SENSOR_TYPES
|
||||
SOCKET_SENSOR_TYPES
|
||||
if isinstance(mill_device, mill.Socket)
|
||||
else HEATER_SENSOR_TYPES
|
||||
if isinstance(mill_device, mill.Heater)
|
||||
else SENSOR_TYPES
|
||||
)
|
||||
|
@ -23,3 +23,20 @@ set_room_temperature:
|
||||
min: 0
|
||||
max: 100
|
||||
unit_of_measurement: "°"
|
||||
|
||||
max_heating_power:
|
||||
fields:
|
||||
entity_id:
|
||||
required: true
|
||||
example: "climate.house"
|
||||
selector:
|
||||
entity:
|
||||
integration: mill
|
||||
domain: climate
|
||||
max_heating_power:
|
||||
required: true
|
||||
selector:
|
||||
number:
|
||||
min: 0
|
||||
max: 2000
|
||||
unit_of_measurement: "W"
|
||||
|
@ -53,6 +53,20 @@
|
||||
}
|
||||
},
|
||||
"services": {
|
||||
"max_heating_power": {
|
||||
"description": "Sets max power heater can use.",
|
||||
"fields": {
|
||||
"entity_id": {
|
||||
"description": "Entity id of heater to change.",
|
||||
"name": "Entity id"
|
||||
},
|
||||
"max_heating_power": {
|
||||
"description": "Max heating power.",
|
||||
"name": "Max heating power"
|
||||
}
|
||||
},
|
||||
"name": "Set max heating power"
|
||||
},
|
||||
"set_room_temperature": {
|
||||
"name": "Set room temperature",
|
||||
"description": "Sets Mill room temperatures.",
|
||||
|
@ -1373,7 +1373,7 @@ microBeesPy==0.3.2
|
||||
mill-local==0.3.0
|
||||
|
||||
# homeassistant.components.mill
|
||||
millheater==0.11.8
|
||||
millheater==0.12.0
|
||||
|
||||
# homeassistant.components.minio
|
||||
minio==7.1.12
|
||||
|
@ -1142,7 +1142,7 @@ microBeesPy==0.3.2
|
||||
mill-local==0.3.0
|
||||
|
||||
# homeassistant.components.mill
|
||||
millheater==0.11.8
|
||||
millheater==0.12.0
|
||||
|
||||
# homeassistant.components.minio
|
||||
minio==7.1.12
|
||||
|
Loading…
x
Reference in New Issue
Block a user