forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add power strip with 2 outlets to kitchen_sink (home-assistant#110346)
- Loading branch information
1 parent
3086d24
commit e27e799
Showing
9 changed files
with
625 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Demo platform that offers a fake button entity.""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.components import persistent_notification | ||
from homeassistant.components.button import ButtonEntity | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.device_registry import DeviceInfo | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from . import DOMAIN | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up the demo button platform.""" | ||
async_add_entities( | ||
[ | ||
DemoButton( | ||
unique_id="2_ch_power_strip", | ||
device_name="2CH Power strip", | ||
entity_name="Restart", | ||
), | ||
] | ||
) | ||
|
||
|
||
class DemoButton(ButtonEntity): | ||
"""Representation of a demo button entity.""" | ||
|
||
_attr_has_entity_name = True | ||
_attr_should_poll = False | ||
|
||
def __init__( | ||
self, | ||
unique_id: str, | ||
device_name: str, | ||
entity_name: str | None, | ||
) -> None: | ||
"""Initialize the Demo button entity.""" | ||
self._attr_unique_id = unique_id | ||
self._attr_device_info = DeviceInfo( | ||
identifiers={(DOMAIN, unique_id)}, | ||
name=device_name, | ||
) | ||
self._attr_name = entity_name | ||
|
||
async def async_press(self) -> None: | ||
"""Send out a persistent notification.""" | ||
persistent_notification.async_create( | ||
self.hass, "Button pressed", title="Button" | ||
) | ||
self.hass.bus.async_fire("demo_button_pressed") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""Create device without entities.""" | ||
|
||
from __future__ import annotations | ||
|
||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import device_registry as dr | ||
|
||
from . import DOMAIN | ||
|
||
|
||
def async_create_device( | ||
hass: HomeAssistant, | ||
config_entry_id: str, | ||
device_name: str | None, | ||
unique_id: str, | ||
) -> dr.DeviceEntry: | ||
"""Create a device.""" | ||
device_registry = dr.async_get(hass) | ||
return device_registry.async_get_or_create( | ||
config_entry_id=config_entry_id, | ||
name=device_name, | ||
identifiers={(DOMAIN, unique_id)}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"""Demo platform that has some fake switches.""" | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.device_registry import DeviceInfo | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from . import DOMAIN | ||
from .device import async_create_device | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up the demo switch platform.""" | ||
async_create_device( | ||
hass, config_entry.entry_id, "2CH Power strip", "2_ch_power_strip" | ||
) | ||
|
||
async_add_entities( | ||
[ | ||
DemoSwitch( | ||
unique_id="outlet_1", | ||
device_name="Outlet 1", | ||
entity_name=None, | ||
state=False, | ||
assumed=False, | ||
via_device="2_ch_power_strip", | ||
), | ||
DemoSwitch( | ||
unique_id="outlet_2", | ||
device_name="Outlet 2", | ||
entity_name=None, | ||
state=True, | ||
assumed=False, | ||
via_device="2_ch_power_strip", | ||
), | ||
] | ||
) | ||
|
||
|
||
class DemoSwitch(SwitchEntity): | ||
"""Representation of a demo switch.""" | ||
|
||
_attr_has_entity_name = True | ||
_attr_should_poll = False | ||
|
||
def __init__( | ||
self, | ||
*, | ||
unique_id: str, | ||
device_name: str, | ||
entity_name: str | None, | ||
state: bool, | ||
assumed: bool, | ||
translation_key: str | None = None, | ||
device_class: SwitchDeviceClass | None = None, | ||
via_device: str | None = None, | ||
) -> None: | ||
"""Initialize the Demo switch.""" | ||
self._attr_assumed_state = assumed | ||
self._attr_device_class = device_class | ||
self._attr_translation_key = translation_key | ||
self._attr_is_on = state | ||
self._attr_unique_id = unique_id | ||
self._attr_device_info = DeviceInfo( | ||
identifiers={(DOMAIN, unique_id)}, | ||
name=device_name, | ||
) | ||
if via_device: | ||
self._attr_device_info["via_device"] = (DOMAIN, via_device) | ||
self._attr_name = entity_name | ||
|
||
def turn_on(self, **kwargs: Any) -> None: | ||
"""Turn the switch on.""" | ||
self._attr_is_on = True | ||
self.schedule_update_ha_state() | ||
|
||
def turn_off(self, **kwargs: Any) -> None: | ||
"""Turn the device off.""" | ||
self._attr_is_on = False | ||
self.schedule_update_ha_state() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.