-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Initial services buildout (#142)
* Inital services buildout. * Inital services buildout. * Update README.md Add some documentation for new service features.
- Loading branch information
Showing
5 changed files
with
251 additions
and
11 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
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,20 @@ | ||
""" Constants used by other files """ | ||
|
||
DOMAIN = "entity_controller" | ||
|
||
# services | ||
SERVICE_CLEAR_BLOCK = "clear_block" | ||
SERVICE_SET_STAY_ON = "set_stay_on" | ||
SERVICE_SET_STAY_OFF = "set_stay_off" | ||
SERVICE_SET_NIGHT_MODE = "set_night_mode" | ||
|
||
#configuration | ||
CONF_START_TIME = 'start_time' | ||
CONF_END_TIME = 'end_time' | ||
|
||
MODE_DAY = 'day' | ||
MODE_NIGHT = 'night' | ||
|
||
STATES = ['idle', 'overridden', 'constrained', 'blocked', | ||
{'name': 'active', 'children': ['timer', 'stay_on'], | ||
'initial': False}] |
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,99 @@ | ||
""" Entity Service Definitions """ | ||
|
||
import voluptuous as vol | ||
|
||
import homeassistant.helpers.config_validation as cv | ||
from homeassistant.helpers.config_validation import ( # noqa: F401 | ||
make_entity_service_schema, | ||
) | ||
|
||
from homeassistant.helpers.entity_component import EntityComponent | ||
|
||
from homeassistant.const import ( | ||
STATE_OFF, | ||
STATE_ON, | ||
) | ||
|
||
import homeassistant.util.dt as dt_util | ||
|
||
from .const import ( | ||
SERVICE_CLEAR_BLOCK, | ||
SERVICE_SET_STAY_ON, | ||
SERVICE_SET_STAY_OFF, | ||
SERVICE_SET_NIGHT_MODE, | ||
CONF_START_TIME, | ||
CONF_END_TIME, | ||
) | ||
|
||
TIME_STR_FORMAT = "%H:%M:%S" | ||
|
||
def async_setup_entity_services(component: EntityComponent): | ||
""" Setup Entity services.""" | ||
|
||
component.logger.debug("Setting up entity services") | ||
component.async_register_entity_service(SERVICE_CLEAR_BLOCK, {}, "async_clear_block") | ||
component.async_register_entity_service(SERVICE_SET_STAY_ON, {}, "async_set_stay_on") | ||
component.async_register_entity_service(SERVICE_SET_STAY_OFF, {}, "async_set_stay_off") | ||
component.async_register_entity_service( | ||
SERVICE_SET_NIGHT_MODE, | ||
{ vol.Optional(CONF_START_TIME): cv.string, vol.Optional(CONF_END_TIME): cv.string }, | ||
"async_set_night_mode") | ||
|
||
return True | ||
|
||
def async_entity_service_clear_block(self): | ||
""" Clear the block property, if set""" | ||
|
||
if(self.model is None or self.model.state != 'blocked'): | ||
return | ||
|
||
self.model.log.debug("Clearing Blocked state") | ||
self.model.block_timer_expires() | ||
|
||
def async_entitiy_service_set_stay_on(self): | ||
""" Changes the stay attribute with a custom value """ | ||
|
||
self.model.log.debug("Changing stay to on") | ||
self.model.stay = True | ||
|
||
def async_entitiy_service_set_stay_off(self): | ||
""" Changes the stay attribute with a custom value """ | ||
|
||
self.model.log.debug("Changing stay to off") | ||
self.model.stay = False | ||
|
||
def async_entity_service_set_night_mode(self, start_time=None, end_time=None): | ||
""" Changes the night mode start and/or end times """ | ||
|
||
if(self.model is None or self.model.night_mode is None): | ||
return | ||
|
||
now = None | ||
if(start_time == 'now'): | ||
if now is None: | ||
now = dt_util.utcnow() | ||
start_time = dt_util.as_local(now).strftime(TIME_STR_FORMAT) | ||
elif(start_time == 'constraint'): | ||
start_time = self.model._start_time_private | ||
|
||
if(end_time == 'now'): | ||
if now is None: | ||
now = dt_util.utcnow() | ||
end_time = dt_util.as_local(now).strftime(TIME_STR_FORMAT) | ||
elif(end_time == 'constraint'): | ||
end_time = self.model._end_time_private | ||
|
||
if(start_time is None and end_time is None): | ||
start_time = end_time = '00:00:00' | ||
|
||
if(not start_time is None): | ||
self.model.log.debug("Setting Night Mode Start to: %s", start_time) | ||
self.model.night_mode[CONF_START_TIME] = start_time | ||
|
||
if(not end_time is None): | ||
self.model.log.debug("Setting Night Mode End to: %s", end_time) | ||
self.model.night_mode[CONF_END_TIME] = end_time | ||
|
||
self.model.prepare_service_data() | ||
|
||
|
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,35 @@ | ||
clear_block: | ||
description: Clears the blocked state of an Entity Controller, if set | ||
fields: | ||
entity_id: | ||
description: Name(s) of entities to change. | ||
example: 'entity_controller.motion_light' | ||
|
||
set_stay_on: | ||
description: Change the stay to on | ||
fields: | ||
entity_id: | ||
description: Name(s) of entities to change. | ||
example: 'entity_controller.motion_light' | ||
|
||
set_stay_off: | ||
description: Change the stay to off | ||
fields: | ||
entity_id: | ||
description: Name(s) of entities to change. | ||
example: 'entity_controller.motion_light' | ||
|
||
set_night_mode: | ||
description: Change the night mode start and end times, does nothing | ||
if night mode is not defined in configuration. If both start_time and end_time | ||
are not provided, it will set both to midnight, effectivly disabling night mode. | ||
fields: | ||
entity_id: | ||
description: Name(s) of entities to change. | ||
example: 'entity_controller.motion_light' | ||
start_time: | ||
description: new start time to set night mode to. | ||
example: sunset - 00:30:00 or '18:30:00' or now or constraint (set same as day start) | ||
end_time: | ||
description: new end time to set night mode to. | ||
example: sunset + 03:00:00 or '21:30:00' or now or constraint (set same as day end) |