Skip to content

Commit

Permalink
Converting the sensor in a switch, and give the possibility to use th…
Browse files Browse the repository at this point in the history
…e switch to activate/disactivate the presence simulation
  • Loading branch information
slashback100 committed Nov 16, 2020
1 parent e774b0f commit 9cc231d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 113 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ You can edit these configurations afterwards by clicking on Options in the integ

# Use it

The component will create an entity called `sensor.presence_simulation`. This entity will be set to `on` when the entity is running. `off` otherwise.
The component will create an entity called `switch.presence_simulation`. This entity will be set to `on` when the entity is running. `off` otherwise.
You have 2 ways of launching the simulation:
## With the switch
Toggling the `switch.presence_simulation` will toggle the presence simulation.

## With the services
Three services are available:
## Start the simulation
The service `presence_simulation.start` will start the simulation and set the `sensor.presence_simulation` entity to `on`.
## Stop the simulation
The service `presence_simulation.stop` will stop the simulation and set the `sensor.presence_simulation` entity to `off`.
## Toggle the simulation
The service `presence_simulation.toggle` will start or stop the simulation, depending on the current state of the `sensor.presence_simulation` entity.
### Start the simulation
The service `presence_simulation.start` will start the simulation and set the `switch.presence_simulation` entity to `on`.
### Stop the simulation
The service `presence_simulation.stop` will stop the simulation and set the `switch.presence_simulation` entity to `off`.
### Toggle the simulation
The service `presence_simulation.toggle` will start or stop the simulation, depending on the current state of the `switch.presence_simulation` entity.
19 changes: 9 additions & 10 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import asyncio
from datetime import datetime,timedelta,timezone
from homeassistant.components import history
from .sensor import PresenceSimulationSwitch
from .const import (
DOMAIN,
SENSOR_PLATFORM,
SENSOR
SWITCH_PLATFORM,
SWITCH
)
_LOGGER = logging.getLogger(__name__)

Expand All @@ -19,7 +18,7 @@ async def async_setup_entry(hass, entry):
unsub = entry.add_update_listener(update_listener)

# Use `hass.async_create_task` to avoid a circular dependency between the platform and the component
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, SENSOR_PLATFORM))
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, SWITCH_PLATFORM))
return await async_mysetup(hass, [entry.data["entities"]], entry.data["delta"])

async def async_setup(hass, config):
Expand All @@ -37,8 +36,8 @@ async def async_mysetup(hass, entities, deltaStr):

async def stop_presence_simulation(err=None):
"""Stop the presence simulation, raising a potential error"""
entity = hass.data[DOMAIN][SENSOR_PLATFORM][SENSOR]
entity.turn_off()
entity = hass.data[DOMAIN][SWITCH_PLATFORM][SWITCH]
entity.internal_turn_off()
if err is not None:
_LOGGER.debug("Error in presence simulation, exiting")
raise e
Expand Down Expand Up @@ -74,13 +73,13 @@ async def async_expand_entities(entities):

async def handle_presence_simulation(call):
"""Start the presence simulation"""
entity = hass.data[DOMAIN][SENSOR_PLATFORM][SENSOR]
entity = hass.data[DOMAIN][SWITCH_PLATFORM][SWITCH]
_LOGGER.debug("Is already running ? %s", entity.state)
if is_running():
_LOGGER.warning("Presence simulation already running")
return
running = True
entity.turn_on()
entity.internal_turn_on()
_LOGGER.debug("Started presence simulation")

current_date = datetime.now(timezone.utc)
Expand Down Expand Up @@ -124,7 +123,7 @@ async def simulate_single_entity(entity_id, hist):
for state in hist: #hypothsis: states are ordered chronologically
_LOGGER.debug("State %s", state.as_dict())
_LOGGER.debug("Switch of %s foreseen at %s", entity_id, state.last_changed+timedelta(delta))
entity = hass.data[DOMAIN][SENSOR_PLATFORM][SENSOR]
entity = hass.data[DOMAIN][SWITCH_PLATFORM][SWITCH]
await entity.async_add_next_event(state.last_changed+timedelta(delta), entity_id, state.state)

while is_running():
Expand Down Expand Up @@ -154,7 +153,7 @@ async def update_entity(entity_id, state):

def is_running():
"""Returns true if the simulation is running"""
entity = hass.data[DOMAIN][SENSOR_PLATFORM][SENSOR]
entity = hass.data[DOMAIN][SWITCH_PLATFORM][SWITCH]
return entity.is_on


Expand Down
4 changes: 2 additions & 2 deletions const.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
DOMAIN = "presence_simulation"
SENSOR_PLATFORM = "sensor"
SENSOR = "presence_simulation"
SWITCH_PLATFORM = "switch"
SWITCH = "presence_simulation"
94 changes: 0 additions & 94 deletions sensor.py

This file was deleted.

0 comments on commit 9cc231d

Please sign in to comment.