Hello,
I have a Raspberry Pi with some custom code that is connected to my garage door motor.
The code is basically a web server that upon receiving a request triggers the motor switch to open or close.
Now, this is working as expected, and I’m now trying to implement a custom component on HA to control it.
I’ve started using the Cover component, and implemented it as a garage_door
platform, and it is showing up on HA.
Now, it is showing on HA as an entity, and not as a device. How can I make it show up as device instead? I’d like to be able to get some extra info from it, like is it open or closed, how much, etc. Also, I think this is also necessary to be able to control it via Google Assistant.
Does anyone have any clue about this?
Thanks in advance for the help!
PS: The code for the component is as follows:
/custom_components/garage_door/__init__.py
"""The garage_door component."""
/custom_components/garage_door/__init__.py
DOMAIN = "garage_door"
/custom_components/garage_door/cover.py
import logging.handlers
import os
import re
import requests
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.core import HomeAssistant as HomeAssistantType
from homeassistant.components.cover import (
CoverEntity,
DEVICE_CLASS_GARAGE,
SUPPORT_OPEN,
SUPPORT_CLOSE,
SUPPORT_STOP
)
from homeassistant.const import CONF_HOST
_LOGGER = logging.getLogger(__name__)
GARAGE_DOOR_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): cv.string,
}
)
def setup_platform(hass: HomeAssistantType, config, add_devices, discovery_info=None):
"""Set up the cover platform"""
add_devices([GarageDoorCover(config.get(CONF_HOST))], True)
class GarageDoorCover(CoverEntity):
def __init__(self, host):
self.host = host
self._unique_id = re.sub(r'[\/\-\._:]+', '-', self.host)
@property
def name(self):
return 'Garage door'
@property
def should_poll(self):
"""If this entity should be polled."""
return True
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
@property
def current_cover_position(self):
return 0
@property
def current_cover_tilt_position(self):
return None
@property
def is_opening(self):
if self._status == None:
return self._status
return False
@property
def is_closing(self):
if self._status == None:
return self._status
return self._status['transitioning']
@property
def is_closed(self):
if self._status == None:
return self._status
return not self._status['open']
@property
def device_class(self):
return DEVICE_CLASS_GARAGE
@property
def unique_id(self):
return self._unique_id
async def async_update(self, **kwargs):
"""Update the door status"""
await self._get_door_status()
async def async_open_cover(self, **kwargs):
"""Open the cover."""
_LOGGER.info('open cover')
await self._async_toggle_door()
async def async_close_cover(self, **kwargs):
"""Open the cover."""
_LOGGER.info('close cover')
await self._async_toggle_door()
async def async_stop_cover(self, **kwargs):
"""Stop the cover."""
_LOGGER.info('stop cover')
async def _get_door_status(self, **kwargs):
url = self.host + '/status'
response = requests.get(url)
if response.status_code == 200:
self._status = response.json()
_LOGGER.info("New status: %s", self._status)
else:
return None
async def _async_toggle_door(self, **kwargs):
url = self.host + '/toggle'
response = requests.post(url)
if response.status_code == 200:
_LOGGER.info("Door toggled successfully")
And it was added to the configuration as:
cover:
- platform: garage_door
host: http://<raspberry-pi-address>