-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
store.py
101 lines (73 loc) · 2.9 KB
/
store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from __future__ import annotations
from .const import LOGGER
from .models import ManagedSwitchConfig
import attr
from homeassistant.helpers.storage import Store
STORAGE_VERSION = 1
STORAGE_ID = "switch_manager"
@attr.s
class SwitchManagerManagedSwitchData:
name = attr.ib(type=str, default='')
enabled = attr.ib(type=bool, default=True)
blueprint = attr.ib(type=str, default=None)
identifier = attr.ib(type=str, default=None)
variables = attr.ib(type=dict, default=None)
rotate = attr.ib(type=int, default=0)
buttons = attr.ib(type=list, default=[])
def set_from_managed_switch_config( self, config: ManagedSwitchConfig ):
self.__dict__.update(config.as_dict())
self.blueprint = config.blueprint.id
@classmethod
def from_dict(cls, data):
return cls(**data)
def asdict(self):
return attr.asdict(self)
@attr.s
class SwitchManagerStoreData:
version = attr.ib(type=str, default="0")
managed_switches = attr.ib(type=dict[str:SwitchManagerManagedSwitchData], factory=dict)
@classmethod
def from_dict(cls, data):
return cls(**data)
def asdict(self):
return attr.asdict(self)
class SwitchManagerStore:
def __init__(self, hass):
self.store = Store(hass, STORAGE_VERSION, STORAGE_ID)
self.data = None
self.dirty = False
async def save(self):
await self.store.async_save(attr.asdict(self.data))
async def load(self):
stored = await self.store.async_load()
if stored:
self.data = SwitchManagerStoreData.from_dict(stored)
# We assume either the stored data was deleteed or is a new install
if self.data is None:
self.data = SwitchManagerStoreData()
await self.save()
self.dirty = False
async def updated(self):
self.dirty = True
await self.save()
async def get_managed_switches(self):
return self.data.managed_switches
def compare_version(self, version):
return self.data.version == str(version)
async def update_version(self, version):
self.data.version = str(version)
await self.updated()
async def delete_managed_switch(self, _id: str):
del self.data.managed_switches[_id]
await self.updated()
def asdict(self):
return self.data.asdict()
def get_available_id( self ) -> str:
if not any(self.data.managed_switches):
return '0'
return str( int(list(self.data.managed_switches.keys())[-1]) + 1 );
async def set_managed_switch( self, config_data: ManagedSwitchConfig ):
config = self.data.managed_switches.get(int(config_data.id), SwitchManagerManagedSwitchData())
config.set_from_managed_switch_config( config_data )
self.data.managed_switches[config_data.id] = config
await self.updated()