Skip to content

Commit 568dfcf

Browse files
Add support for the new Select Entity (#27)
* Adding support for the new select entity * Newline * Update minimum homeassistant version Co-authored-by: Tomas Hellström <tomas.hellstrom@yahoo.se>
1 parent 9ce2911 commit 568dfcf

6 files changed

Lines changed: 80 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ This is a helper integration for NetDaemon, this is not NetDeamon it self, for t
1010
- Service to register custom services to use in NetDaemon
1111
- Service to force a reload of NetDaemon
1212

13-
For now only the folowing platforms are supported:
13+
For now only the following platforms are supported:
1414

1515
- `binary_sensor`
1616
- `sensor`
1717
- `switch`
18+
- `select`
1819
- `climate`
1920

2021
## Installation

custom_components/netdaemon/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
ATTR_ICON,
1212
ATTR_STATE,
1313
ATTR_UNIT,
14+
ATTR_OPTIONS,
1415
LOGGER,
1516
STORAGE_VERSION,
1617
)
@@ -52,6 +53,7 @@ async def entity_create(self, data) -> None:
5253
ATTR_STATE: data.get(ATTR_STATE),
5354
ATTR_ICON: data.get(ATTR_ICON),
5455
ATTR_UNIT: data.get(ATTR_UNIT),
56+
ATTR_OPTIONS: data.get(ATTR_OPTIONS),
5557
ATTR_ATTRIBUTES: data.get(ATTR_ATTRIBUTES, {}),
5658
}
5759
await self.store.async_save(self._entities)

custom_components/netdaemon/const.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
INTEGRATION_VERSION = "main"
77
DOMAIN = "netdaemon"
88
NAME = "NetDaemon"
9-
MINIMUM_HA_VERSION = "2020.12.0"
9+
MINIMUM_HA_VERSION = "2021.7.0"
1010
STORAGE_VERSION = "1"
1111

1212
ND_ID = "86ec6a70-b2b8-427d-8fcf-3f14331dddd7"
@@ -23,6 +23,7 @@
2323
ATTR_UNIT = "unit"
2424
ATTR_ATTRIBUTES = "attributes"
2525
ATTR_VERSION = "version"
26+
ATTR_OPTIONS = "options"
2627
ATTR_TARGET_TEMPERATURE = "target_temperature"
2728
ATTR_TARGET_HUMIDITY = "target_humidity"
2829
ATTR_TEMPERATURE_UNIT = "temperature_unit"
@@ -58,12 +59,14 @@
5859
PLATFORM_BINARY_SENSOR = "binary_sensor"
5960
PLATFORM_SENSOR = "sensor"
6061
PLATFORM_SWITCH = "switch"
62+
PLATFORM_SELECT = "select"
6163
PLATFORM_CLIMATE = "climate"
6264

6365
PLATFORMS = [
6466
PLATFORM_BINARY_SENSOR,
6567
PLATFORM_SENSOR,
6668
PLATFORM_SWITCH,
69+
PLATFORM_SELECT,
6770
PLATFORM_CLIMATE,
6871
]
6972

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""Select platform for NetDaemon."""
2+
from typing import TYPE_CHECKING
3+
4+
from homeassistant.components.select import SelectEntity
5+
6+
from .const import (
7+
ATTR_CLIENT,
8+
ATTR_COORDINATOR,
9+
ATTR_ENTITY_ID,
10+
ATTR_STATE,
11+
ATTR_OPTIONS,
12+
DOMAIN,
13+
LOGGER,
14+
PLATFORM_SELECT,
15+
)
16+
from .entity import NetDaemonEntity
17+
18+
if TYPE_CHECKING:
19+
from homeassistant.config_entries import ConfigEntry
20+
from homeassistant.core import HomeAssistant
21+
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
22+
23+
from .client import NetDaemonClient
24+
25+
async def async_setup_entry(
26+
hass: "HomeAssistant", _config_entry: "ConfigEntry", async_add_devices
27+
) -> None:
28+
"""Setup select platform."""
29+
client: "NetDaemonClient" = hass.data[DOMAIN][ATTR_CLIENT]
30+
coordinator: "DataUpdateCoordinator" = hass.data[DOMAIN][ATTR_COORDINATOR]
31+
32+
selects = []
33+
for entity in client.entities:
34+
if entity.split(".")[0] == PLATFORM_SELECT:
35+
LOGGER.debug("Adding %s", entity)
36+
selects.append(
37+
NetDaemonSelect(coordinator, entity.split(".")[1])
38+
)
39+
40+
if selects:
41+
async_add_devices(selects)
42+
43+
44+
class NetDaemonSelect(NetDaemonEntity, SelectEntity):
45+
"""NetDaemon select class."""
46+
47+
@property
48+
def current_option(self):
49+
"""Return the state of the select."""
50+
if not self.entity_id:
51+
return None
52+
state = str(self._coordinator.data[self.entity_id][ATTR_STATE])
53+
return state
54+
55+
@property
56+
def options(self):
57+
"""Return the list of available options."""
58+
if not self.entity_id:
59+
return None
60+
return self._coordinator.data[self.entity_id][ATTR_OPTIONS]
61+
62+
async def async_select_option(self, option: str) -> None:
63+
"""Change the selected option."""
64+
await self.hass.data[DOMAIN][ATTR_CLIENT].entity_update(
65+
{ATTR_ENTITY_ID: self.entity_id, ATTR_STATE: option}
66+
)
67+
self.async_write_ha_state()

custom_components/netdaemon/services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ entity_create:
2525
example: "mdi:rocket-launch-outline"
2626
unit:
2727
description: The unit of measurement for the entity
28+
options:
29+
description: List of options for a select entity
2830
attributes:
2931
description: The attributes of the entity
3032

@@ -42,6 +44,8 @@ entity_update:
4244
example: "mdi:rocket-launch-outline"
4345
unit:
4446
description: The unit of measurement for the entity
47+
options:
48+
description: List of options for a select entity
4549
attributes:
4650
description: The attributes of the entity
4751

hacs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"filename": "netdaemon.zip",
44
"zip_release": true,
55
"hide_default_branch": true,
6-
"homeassistant": "2020.12.0",
6+
"homeassistant": "2021.7.0",
77
"hacs": "0.19.0"
88
}

0 commit comments

Comments
 (0)