Skip to content

Commit af4050f

Browse files
committed
add select entity and extra sensors
1 parent b059cf6 commit af4050f

File tree

8 files changed

+181
-12
lines changed

8 files changed

+181
-12
lines changed

custom_components/smarter/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
Platform.NUMBER,
2222
Platform.SENSOR,
2323
Platform.SWITCH,
24+
Platform.SELECT,
2425
]
2526

2627
SERVICE_QUICK_BOIL = "quick_boil"

custom_components/smarter/devices/smarter_coffee_SMCOF01.yaml

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ primary_entity:
1010
secondary_entities:
1111
- entity: sensor
1212
translation_key: brewed_keeping_warm
13-
state_field: status.brewed_keeping_warm
14-
state_class: measurement
13+
state_field: brewed_keeping_warm.ts
14+
device_class: timestamp
15+
enabled_default: false
1516
- entity: sensor
1617
translation_key: brewing_started
17-
state_field: status.brewing_started
18-
state_class: measurement
18+
state_field: brewing_started.ts
19+
enabled_default: false
20+
unit: datetime
1921
- entity: sensor
2022
translation_key: number_of_cups_since_descale
2123
state_class: measurement
@@ -47,15 +49,35 @@ secondary_entities:
4749
icon: "mdi:cup"
4850
- entity: binary_sensor
4951
translation_key: carafe_is_present
52+
- entity: select
53+
translation_key: coffee_strength
54+
state_field: coffee_strength
55+
setter: set_coffee_strength
56+
mapping:
57+
- value: 20
58+
native_value: "Weak"
59+
- value: 50
60+
native_value: "Medium"
61+
- value: 80
62+
native_value: "Strong"
63+
setter_mapping:
64+
- value: 20
65+
native_value: "Weak"
66+
- value: 50
67+
native_value: "Medium"
68+
- value: 80
69+
native_value: "Strong"
5070
- entity: number
5171
translation_key: coffee_strength
52-
unit: "%"
5372
state_class: measurement
5473
setter: set_coffee_strength
55-
range:
56-
min: 20
57-
max: 80
58-
step: 30
74+
mapping:
75+
- value: 20
76+
native_value: "Weak"
77+
- value: 50
78+
native_value: "Medium"
79+
- value: 80
80+
native_value: "Strong"
5981
icon: "mdi:wifi-strength-1"
6082
- entity: number
6183
translation_key: number_of_cups

custom_components/smarter/devices/smarter_kettle_SMKET01.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ primary_entity:
88
translation_key: state
99
icon: "mdi:kettle"
1010
secondary_entities:
11+
- entity: sensor
12+
translation_key: boiling_started
13+
state_field: boiling_started.ts
14+
device_class: timestamp
15+
enabled_default: false
16+
- entity: sensor
17+
translation_key: keep_warm_finished
18+
state_field: keep_warm_finished.ts
19+
device_class: timestamp
20+
enabled_default: false
21+
- entity: sensor
22+
translation_key: cooled_keeping_warm
23+
state_field: cooled_keeping_warm.ts
24+
device_class: timestamp
25+
enabled_default: false
26+
- entity: sensor
27+
translation_key: boiled_keeping_warm
28+
state_field: boiled_keeping_warm.ts
29+
device_class: timestamp
30+
enabled_default: false
1131
- entity: sensor
1232
translation_key: "water_temperature"
1333
unit: C
@@ -112,3 +132,9 @@ secondary_entities:
112132
min: 0
113133
max: 40
114134
step: 1
135+
- entity: sensor
136+
translation_key: keep_warm_time_remaining
137+
unit: min
138+
device_class: duration
139+
state_class: measurement
140+
icon: "mdi:clock"

custom_components/smarter/helpers/device_config.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
import logging
66
from collections.abc import Iterable
7+
from datetime import datetime
78
from fnmatch import fnmatch
89
from os import walk
910
from os.path import dirname, join, splitext
1011
from typing import Any
1112

1213
from homeassistant.components.binary_sensor import BinarySensorEntityDescription
1314
from homeassistant.components.number import NumberEntityDescription, NumberMode
14-
from homeassistant.components.sensor import SensorEntityDescription
15+
from homeassistant.components.select import SelectEntityDescription
16+
from homeassistant.components.sensor import SensorDeviceClass, SensorEntityDescription
1517
from homeassistant.components.switch import SwitchEntityDescription
1618
from homeassistant.const import EntityCategory, Platform, UnitOfTemperature
1719
from homeassistant.helpers.entity import EntityDescription
@@ -221,11 +223,31 @@ def max(self):
221223
"""Return maximum valid value."""
222224
return self._range["max"]
223225

226+
@property
227+
def entity_registry_enabled_default(self):
228+
"""Return true if entity should be enabled by default."""
229+
return self._config.get("enabled_default", True)
230+
231+
@property
232+
def entity_registry_visible_default(self):
233+
"""Return true if entity should be visible by default."""
234+
return self.entity_registry_enabled_default and self._config.get("visible_default", True)
235+
236+
@property
237+
def options(self):
238+
"""Return options for select entity."""
239+
return [mapping["value"] for mapping in self._mappings]
240+
224241
@property
225242
def _mappings(self):
226243
return self._config.get("mapping", [])
227244

228245
def _get_value_for_native(self, native_value: Any):
246+
if self.device_class == SensorDeviceClass.TIMESTAMP:
247+
now = datetime.now()
248+
local_now = now.astimezone()
249+
local_tz = local_now.tzinfo
250+
return datetime.fromtimestamp(native_value, tz=local_tz)
229251
for mapping in self._mappings:
230252
if mapping.get("native_value") == native_value:
231253
value = mapping.get("value")
@@ -343,6 +365,8 @@ def entity_description(self) -> EntityDescription:
343365
translation_key=self.translation_key,
344366
translation_placeholders=self.translation_placeholders,
345367
name=None if self.is_primary else self.name,
368+
entity_registry_enabled_default=self.entity_registry_enabled_default,
369+
entity_registry_visible_default=self.entity_registry_visible_default,
346370
has_entity_name=True,
347371
)
348372

@@ -367,6 +391,14 @@ def switch_entity_description(self) -> SwitchEntityDescription:
367391
"""Return switch entity description."""
368392
return SwitchEntityDescription(**self.entity_description.__dict__)
369393

394+
@property
395+
def select_entity_description(self) -> SelectEntityDescription:
396+
"""Return select entity description."""
397+
return SelectEntityDescription(
398+
key=self.key,
399+
options=[mapping["value"] for mapping in self._mappings],
400+
)
401+
370402
@property
371403
def number_entity_description(self) -> NumberEntityDescription:
372404
"""Return number entity description."""

custom_components/smarter/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
"smarter-client==0.3.0-dev.3"
1818
],
1919
"ssdp": [],
20-
"version": "0.4.0-dev.10",
20+
"version": "0.4.0-dev.11",
2121
"zeroconf": []
2222
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Support for Smarter number fields."""
2+
3+
from __future__ import annotations
4+
5+
from homeassistant.components.select import SelectEntity, SelectEntityDescription
6+
from homeassistant.config_entries import ConfigEntry
7+
8+
# from custom_components.smarter.entity import (
9+
# SmarterEntity,
10+
# SmarterSensorEntityDescription,
11+
# )
12+
# from custom_components.smarter.helpers.base import ServiceMetadata
13+
from homeassistant.const import Platform
14+
from homeassistant.core import HomeAssistant
15+
from homeassistant.helpers.entity_platform import AddEntitiesCallback
16+
from smarter_client.managed_devices.base import BaseDevice
17+
18+
from custom_components.smarter.helpers.device_config import SmarterEntityConfig
19+
20+
# from custom_components.smarter.entity import SmarterEntity
21+
# from custom_components.smarter.helpers.config import async_setup_smarter_platform
22+
# from custom_components.smarter.helpers.device_config import SmarterEntityConfig
23+
from .entity import SmarterEntity
24+
from .helpers.config import async_setup_smarter_platform
25+
26+
# from custom_components.smarter.entity import (
27+
# SmarterEntity,
28+
# SmarterSensorEntityDescription,
29+
# )
30+
# from custom_components.smarter.helpers.base import ServiceMetadata
31+
32+
33+
async def async_setup_entry(
34+
hass: HomeAssistant,
35+
config_entry: ConfigEntry,
36+
async_add_entities: AddEntitiesCallback,
37+
) -> None:
38+
"""Set up Smarter sensors."""
39+
data = {**config_entry.data, **config_entry.options}
40+
await async_setup_smarter_platform(
41+
hass,
42+
data,
43+
async_add_entities,
44+
Platform.SELECT,
45+
SmarterSelect,
46+
)
47+
48+
49+
class SmarterSelect(SmarterEntity, SelectEntity):
50+
"""Representation of a Smarter number."""
51+
52+
entity_description: SelectEntityDescription
53+
_attr_has_entity_name = True
54+
55+
def __init__(self, device: BaseDevice, config: SmarterEntityConfig):
56+
"""Create instance."""
57+
super().__init__(device, config, config.select_entity_description)
58+
59+
@property
60+
def options(self):
61+
"""Return the list of possible options."""
62+
return self.config.options
63+
64+
@property
65+
def current_option(self):
66+
"""Return the currently selected option."""
67+
return self.config.get_value(self.device)
68+
69+
def select_option(self, option):
70+
"""Set the option."""
71+
self.config.set_value(self.device, option)

custom_components/smarter/translations/en.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,24 @@
5858
}
5959
},
6060
"entity": {
61+
"select": {
62+
"coffee_strength": {
63+
"name": "Coffee Strength"
64+
}
65+
},
6166
"sensor": {
67+
"keep_warm_finished": {
68+
"name": "Keep Warm Finished"
69+
},
70+
"cooled_keeping_warm": {
71+
"name": "Cooled Keeping Warm"
72+
},
73+
"boiled_keeping_warm": {
74+
"name": "Boiled Keeping Warm"
75+
},
76+
"boiling_started": {
77+
"name": "Boiling started"
78+
},
6279
"water_temperature": {
6380
"name": "Water temperature"
6481
},

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ requires-python = "~=3.12.4"
44
readme = "README.md"
55
license = { text = "MIT" }
66
name = "homeassistant-smarter"
7-
version = "0.4.0-dev.10"
7+
version = "0.4.0-dev.11"
88
description = "Default template for PDM package"
99
authors = [{ name = "Kirill Birger", email = "[email protected]" }]
1010

0 commit comments

Comments
 (0)