Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to support HA 2022.x #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 62 additions & 28 deletions custom_components/cover_time_based/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
from datetime import timedelta

from homeassistant.core import callback
from homeassistant.helpers import entity_platform
from homeassistant.helpers.event import async_track_utc_time_change, async_track_time_interval
from homeassistant.components.cover import (
ATTR_CURRENT_POSITION,
ATTR_POSITION,
PLATFORM_SCHEMA,
DEVICE_CLASSES_SCHEMA,
CoverEntity,
)
from homeassistant.const import (
CONF_NAME,
CONF_DEVICE_CLASS,
ATTR_ENTITY_ID,
ATTR_DEVICE_CLASS,
SERVICE_CLOSE_COVER,
SERVICE_OPEN_COVER,
SERVICE_STOP_COVER,
Expand All @@ -26,7 +31,6 @@
_LOGGER = logging.getLogger(__name__)

CONF_DEVICES = 'devices'
CONF_NAME = 'name'
CONF_ALIASES = 'aliases'
CONF_TRAVELLING_TIME_DOWN = 'travelling_time_down'
CONF_TRAVELLING_TIME_UP = 'travelling_time_up'
Expand All @@ -35,14 +39,16 @@
CONF_OPEN_SWITCH_ENTITY_ID = 'open_switch_entity_id'
CONF_CLOSE_SWITCH_ENTITY_ID = 'close_switch_entity_id'

SERVICE_SET_KNOWN_POSITION = 'set_known_position'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_DEVICES, default={}): vol.Schema(
{
cv.string: {
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_OPEN_SWITCH_ENTITY_ID): cv.string,
vol.Optional(CONF_CLOSE_SWITCH_ENTITY_ID): cv.string,
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_OPEN_SWITCH_ENTITY_ID): cv.string,
vol.Required(CONF_CLOSE_SWITCH_ENTITY_ID): cv.string,
vol.Optional(CONF_ALIASES, default=[]):
vol.All(cv.ensure_list, [cv.string]),

Expand All @@ -56,6 +62,15 @@
}
)

POSITION_SCHEMA = vol.Schema(
{
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
vol.Required(ATTR_POSITION): cv.positive_int,
}
)

DOMAIN = "cover_time_based"

def devices_from_config(domain_config):
"""Parse configuration and add cover devices."""
devices = []
Expand All @@ -73,16 +88,22 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
"""Set up the cover platform."""
async_add_entities(devices_from_config(config))

class CoverTimeBased(CoverEntity, RestoreEntity):

platform = entity_platform.current_platform.get()

platform.async_register_entity_service(
SERVICE_SET_KNOWN_POSITION, POSITION_SCHEMA, "set_known_position"
)

class CoverTimeBased(CoverEntity, RestoreEntity):
def __init__(self, device_id, name, travel_time_down, travel_time_up, open_switch_entity_id, close_switch_entity_id):
"""Initialize the cover."""
from xknx.devices import TravelCalculator
self._travel_time_down = travel_time_down
self._travel_time_up = travel_time_up
self._open_switch_entity_id = open_switch_entity_id
self._close_switch_entity_id = close_switch_entity_id

self._unique_id = device_id

if name:
self._name = name
else:
Expand All @@ -97,17 +118,13 @@ async def async_added_to_hass(self):
""" The rest is calculated from this attribute."""
old_state = await self.async_get_last_state()
_LOGGER.debug('async_added_to_hass :: oldState %s', old_state)
if (
old_state is not None and
self.tc is not None and
old_state.attributes.get(ATTR_CURRENT_POSITION) is not None):
self.tc.set_position(int(
old_state.attributes.get(ATTR_CURRENT_POSITION)))

def _handle_my_button(self):
"""Handle the MY button press"""
if (old_state is not None and self.tc is not None and old_state.attributes.get(ATTR_CURRENT_POSITION) is not None):
self.tc.set_position(int(old_state.attributes.get(ATTR_CURRENT_POSITION)))

def _handle_stop(self):
"""Handle stop"""
if self.tc.is_traveling():
_LOGGER.debug('_handle_my_button :: button stops cover')
_LOGGER.debug('_handle_stop :: button stops cover')
self.tc.stop()
self.stop_auto_updater()

Expand All @@ -117,7 +134,17 @@ def name(self):
return self._name

@property
def device_state_attributes(self):
def unique_id(self):
"""Return the unique id."""
return "cover_timebased_uuid_" + self._unique_id

@property
def device_class(self):
"""Return the device class of the cover."""
return "shutter"

@property
def extra_state_attributes(self):
"""Return the device state attributes."""
attr = {}
if self._travel_time_down is not None:
Expand Down Expand Up @@ -165,23 +192,24 @@ async def async_set_cover_position(self, **kwargs):
async def async_close_cover(self, **kwargs):
"""Turn the device close."""
_LOGGER.debug('async_close_cover')
self.tc.start_travel_down()

self.start_auto_updater()
await self._async_handle_command(SERVICE_CLOSE_COVER)
if self.tc.current_position() > 0:
self.tc.start_travel_down()
self.start_auto_updater()
await self._async_handle_command(SERVICE_CLOSE_COVER)

async def async_open_cover(self, **kwargs):
"""Turn the device open."""
_LOGGER.debug('async_open_cover')
self.tc.start_travel_up()

self.start_auto_updater()
await self._async_handle_command(SERVICE_OPEN_COVER)
if self.tc.current_position() < 100:
self.tc.start_travel_up()
self.start_auto_updater()
await self._async_handle_command(SERVICE_OPEN_COVER)

async def async_stop_cover(self, **kwargs):
"""Turn the device stop."""
_LOGGER.debug('async_stop_cover')
self._handle_my_button()
self._handle_stop()
await self._async_handle_command(SERVICE_STOP_COVER)

async def set_position(self, position):
Expand Down Expand Up @@ -236,9 +264,15 @@ async def auto_stop_if_necessary(self):
"""Do auto stop if necessary."""
if self.position_reached():
_LOGGER.debug('auto_stop_if_necessary :: calling stop command')
await self._async_handle_command(SERVICE_STOP_COVER)
self.tc.stop()

await self._async_handle_command(SERVICE_STOP_COVER)

async def set_known_position(self, **kwargs):
"""We want to do a few things when we get a position"""
position = kwargs[ATTR_POSITION]
self._handle_stop()
await self._async_handle_command(SERVICE_STOP_COVER)
self.tc.set_position(position)

async def _async_handle_command(self, command, *args):
if command == "close_cover":
Expand Down
6 changes: 3 additions & 3 deletions custom_components/cover_time_based/manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"domain": "cover_time_based",
"name": "Cover Time Based",
"version": "1.0.0",
"documentation": "https://github.com/davidramosweb/home-assistant-custom-components-cover-time-based",
"version": "1.1.2",
"documentation": "https://github.com/dankocrnkovic/home-assistant-custom-components-cover-time-based",
"requirements": [
"xknx==0.9.4"
],
"codeowners": ["@davidramosweb"]
"codeowners": ["@davidramosweb", "@dankocrnkovic"]
}
9 changes: 9 additions & 0 deletions custom_components/cover_time_based/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ send_command:
fields:
command: {description: The command to be sent., example: 'open_cover'}
device_id: {description: device ID.}
set_known_position:
description: Sets cover internal position
fields:
entity_id:
description: entity id of cover to set position for
example: cover.garage_door
position:
description: position of cover, between 0 and 100
example: 50
7 changes: 7 additions & 0 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Cover Time Based",
"content_in_root": false,
"render_readme": true,
"domains": ["cover"],
"homeassistant": "2022.4"
}