Skip to content

Instantly share code, notes, and snippets.

@AlexxIT
Created January 25, 2020 18:33
Show Gist options
  • Save AlexxIT/58ad8a3922da0e7adaa43dd41fe022c3 to your computer and use it in GitHub Desktop.
Save AlexxIT/58ad8a3922da0e7adaa43dd41fe022c3 to your computer and use it in GitHub Desktop.
# custom_components/boiler/__init__.py
sensor:
- platform: boiler
- platform: template
sensors:
title:
value_template: "{{ state_attr('sensor.boiler', 'title') }}"
# custom_components/boiler/sensor.py
import logging
import re
import requests
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
add_entities([BoilerSensor()])
class BoilerSensor(Entity):
def __init__(self):
self._state = None
self._attr = None
@property
def entity_id(self):
return 'sensor.boiler'
@property
def state(self):
return self._state
@property
def state_attributes(self):
return self._attr
def update(self):
_LOGGER.info("Update")
r = requests.get("https://www.google.com/")
m = re.search(r'<title>(.+?)</title>', r.text)
self._state = m[1]
self._attr = {
'title': m[1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment